Class: Bolt::ProjectManager

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt/project_manager.rb,
lib/bolt/project_manager/migrator.rb,
lib/bolt/project_manager/config_migrator.rb,
lib/bolt/project_manager/module_migrator.rb,
lib/bolt/project_manager/inventory_migrator.rb

Defined Under Namespace

Classes: ConfigMigrator, InventoryMigrator, Migrator, ModuleMigrator

Constant Summary collapse

INVENTORY_TEMPLATE =
<<~INVENTORY
  # This is an example inventory.yaml
  # To read more about inventory files, see https://pup.pt/bolt-inventory
  #
  # groups:
  #   - name: linux
  #     targets:
  #       - target1.example.com
  #       - target2.example.com
  #     config:
  #       transport: ssh
  #       ssh:
  #         private-key: /path/to/private_key.pem
  #   - name: windows
  #     targets:
  #       - name: win1
  #         uri: target3.example.com
  #       - name: win2
  #         uri: target4.example.com
  #     config:
  #       transport: winrm
  # config:
  #   ssh:
  #     host-key-check: false
  #   winrm:
  #     user: Administrator
  #     password: Bolt!
  #     ssl: false
INVENTORY
GITIGNORE_CONTENT =
<<~GITIGNORE
  .modules/
  .resource_types/
  bolt-debug.log
  .plan_cache.json
  .plugin_cache.json
  .task_cache.json
  .rerun.json
GITIGNORE

Instance Method Summary collapse

Constructor Details

#initialize(config, outputter, pal) ⇒ ProjectManager

Returns a new instance of ProjectManager.



49
50
51
52
53
# File 'lib/bolt/project_manager.rb', line 49

def initialize(config, outputter, pal)
  @config    = config
  @outputter = outputter
  @pal       = pal
end

Instance Method Details

#create(path, name, modules) ⇒ Object

Creates a new project at the specified directory.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/bolt/project_manager.rb', line 57

def create(path, name, modules)
  require_relative '../bolt/module_installer'

  project       = Pathname.new(File.expand_path(path))
  old_config    = project + 'bolt.yaml'
  config        = project + 'bolt-project.yaml'
  puppetfile    = project + 'Puppetfile'
  moduledir     = project + '.modules'
  inventoryfile = project + 'inventory.yaml'
  gitignore     = project + '.gitignore'
  project_name  = name || File.basename(project)

  if config.exist?
    if modules
      command = Bolt::Util.powershell? ? 'Add-BoltModule -Module' : 'bolt module add'
      raise Bolt::Error.new(
        "Found existing project directory with #{config.basename} at #{project}, "\
        "unable to initialize project with modules. To add modules to the project, "\
        "run '#{command} <module>' instead.",
        'bolt/existing-project-error'
      )
    else
      raise Bolt::Error.new(
        "Found existing project directory with #{config.basename} at #{project}, "\
        "unable to initialize project.",
        'bolt/existing-project-error'
      )
    end
  elsif old_config.exist?
    command = Bolt::Util.powershell? ? 'Update-BoltProject' : 'bolt project migrate'
    raise Bolt::Error.new(
      "Found existing project directory with #{old_config.basename} at #{project}, "\
      "unable to initialize project. #{old_config.basename} is deprecated. To "\
      "update the project to current best practices, run '#{command}'.",
      'bolt/existing-project-error'
    )
  elsif modules && puppetfile.exist?
    raise Bolt::Error.new(
      "Found existing Puppetfile at #{puppetfile}, unable to initialize project "\
      "with modules.",
      'bolt/existing-puppetfile-error'
    )
  elsif project_name !~ Bolt::Module::MODULE_NAME_REGEX
    if name
      raise Bolt::ValidationError,
            "The provided project name '#{project_name}' is invalid; project name must "\
            "begin with a lowercase letter and can include lowercase letters, "\
            "numbers, and underscores."
    else
      command = Bolt::Util.powershell? ? 'New-BoltProject -Name' : 'bolt project init'
      raise Bolt::ValidationError,
            "The current directory name '#{project_name}' is an invalid project name. "\
            "Please specify a name using '#{command} <name>'."
    end
  end

  # If modules were specified, resolve and install first. We want to error
  # early here and not initialize the project if the modules cannot be
  # resolved and installed.
  if modules
    @outputter.start_spin
    Bolt::ModuleInstaller.new(@outputter, @pal).install(modules, puppetfile, moduledir)
    @outputter.stop_spin
  end

  data = { 'name' => project_name }
  data['modules'] = modules || []

  begin
    File.write(config.to_path, data.to_yaml)
  rescue StandardError => e
    raise Bolt::FileError.new("Could not create bolt-project.yaml at #{project}: #{e.message}", nil)
  end

  unless inventoryfile.exist?
    begin
      File.write(inventoryfile.to_path, INVENTORY_TEMPLATE)
    rescue StandardError => e
      raise Bolt::FileError.new("Could not create inventory.yaml at #{project}: #{e.message}", nil)
    end
  end

  unless gitignore.exist?
    begin
      File.write(gitignore.to_path, GITIGNORE_CONTENT)
    rescue StandardError => e
      raise Bolt::FileError.new("Could not create .gitignore at #{project}: #{e.message}", nil)
    end
  end

  @outputter.print_message("Successfully created Bolt project at #{project}")

  0
end

#migrateObject

Migrates a project to use the latest file versions and best practices.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/bolt/project_manager.rb', line 154

def migrate
  unless $stdin.tty?
    raise Bolt::Error.new(
      "stdin is not a tty, unable to migrate project",
      'bolt/stdin-not-a-tty-error'
    )
  end

  @outputter.print_message("Migrating project #{@config.project.path}\n\n")

  @outputter.print_action_step(
    "Migrating a Bolt project might make irreversible changes to the project's "\
    "configuration and inventory files. Before continuing, make sure the "\
    "project has a backup or uses a version control system."
  )

  return 0 unless Bolt::Util.prompt_yes_no("Continue with project migration?", @outputter)

  @outputter.print_message('')

  ok = migrate_inventory && migrate_config && migrate_modules

  if ok
    @outputter.print_message("Project successfully migrated")
  else
    @outputter.print_error("Project could not be migrated completely")
  end

  ok ? 0 : 1
end