Class: Opsup::App

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/opsup/app.rb

Constant Summary collapse

AVAILABLE_COMMANDS =
T.let(
  %w[
    upload_cookbooks
    update_cookbooks
    setup
    configure
    deploy
  ].freeze,
  T::Array[String],
)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger:) ⇒ App

Returns a new instance of App.



14
15
16
# File 'lib/opsup/app.rb', line 14

def initialize(logger:)
  @logger = T.let(logger, ::Logger)
end

Class Method Details

.createObject



9
10
11
# File 'lib/opsup/app.rb', line 9

def self.create
  new(logger: Opsup::Logger.instance)
end

Instance Method Details

#available_commandsObject



30
31
32
# File 'lib/opsup/app.rb', line 30

def available_commands
  AVAILABLE_COMMANDS
end

#run(commands, config) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/opsup/app.rb', line 35

def run(commands, config)
  validate_commands(commands)
  @logger.warn('Starting in DRYRUN MODE') if config.dryrun
  @logger.info("Commands: #{commands.join(',')}, Stack: #{config.stack_name}")
  @logger.debug("Configuration details: #{config.to_h}")

  opsworks = new_opsworks_client(config)
  stack_operator = Opsup::StackOperator.create(opsworks: opsworks)
  deployer = stack_operator.new_deployer(
    stack_name: config.stack_name,
    mode: config.running_mode,
    dryrun: config.dryrun,
  )

  commands.each do |command|
    if command == 'upload_cookbooks'
      upload_cookbooks(config)
      next
    end
    deployer.run_command(command_to_opsworks_command(command))
  end
ensure
  msg = 'Finished' + (config.dryrun ? ' (DRYRUN MODE)' : '')
  @logger.info(msg)
end

#upload_cookbooks(config) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/opsup/app.rb', line 82

def upload_cookbooks(config)
  if config.cookbook_url.nil?
    raise Opsup::Error, 'cookbook URL is required to run upload_cookbooks'
  end
  if config.s3_bucket_name.nil?
    raise Opsup::Error, 'S3 Bucket name is required to run upload_cookbooks'
  end

  s3_object_config = CookbookUploader::S3ObjectConfig.new(
    bucket_name: T.must(config.s3_bucket_name),
    key: "cookbook_#{config.stack_name}.tar.gz",
  )

  cookbook_uploader = CookbookUploader.create(s3: new_s3_client(config), config: config)
  cookbook_uploader.build_and_upload(
    cookbook_url: T.must(config.cookbook_url),
    s3_object_config: s3_object_config,
  )
end