Class: Iota::CLI::Deploy

Inherits:
Object
  • Object
show all
Defined in:
lib/iota/cli/deploy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, environment, thor) ⇒ Deploy

Returns a new instance of Deploy.



7
8
9
10
11
12
# File 'lib/iota/cli/deploy.rb', line 7

def initialize(options, environment, thor)
  @options = options
  @environment = environment
  @thor = thor
  @path = options[:path] || '.'
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



5
6
7
# File 'lib/iota/cli/deploy.rb', line 5

def environment
  @environment
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/iota/cli/deploy.rb', line 5

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/iota/cli/deploy.rb', line 5

def path
  @path
end

#thorObject (readonly)

Returns the value of attribute thor.



5
6
7
# File 'lib/iota/cli/deploy.rb', line 5

def thor
  @thor
end

Instance Method Details

#runObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/iota/cli/deploy.rb', line 14

def run
  unless File.exists?('./iota.conf')
    thor.say("Config file ('./iota.conf') is missing.", :red)
    return
  end

  thor.say("Loading configuration.", :yellow)
  config = YAML.load_file('./iota.conf')
  thor.say("Done.", :blue)

  unless Iota::Function::ENVIRONMENT_ALIAS_NAME_MAP.keys.include?(environment)
    thor.say("Invalid environment #{environment}. \\
      Please specify either 'production' or 'development'.", :red)
    return
  end

  # TODO: not found
  function_name = config['function_name']

  function = Iota::Function.new(function_name, path)
  if function.exists?
    thor.say("Updating code...", :yellow)
    function.update_code
    thor.say("Done.", :blue)
    new_version = function.publish_version.version
    thor.say("Latest code is published as version:#{new_version}.", :yellow)

    aliases = function.list_aliases

    new_version_alias_name = Iota::Function::ENVIRONMENT_ALIAS_NAME_MAP[environment]
    last_version_alias_name = new_version_alias_name +
      Iota::Function::LAST_VERSION_SUFFIX

    last_version_alias = aliases.select{|a| a.name == last_version_alias_name}.first
    new_version_alias = aliases.select{|a| a.name == new_version_alias_name}.first

    # last -> prod/dev, prod/dev -> new_version
    unless last_version_alias.nil? || new_version_alias.nil?
      thor.say("Updating aliases...", :yellow)
      function.update_alias(last_version_alias_name, new_version_alias.function_version)
      function.update_alias(new_version_alias_name, new_version)
      thor.say("Done.", :blue)
    else
      thor.say("There's no aliases with name '#{last_version_alias_name}' \
and/or '#{new_version_alias_name}'.", :red)
    end
  else
    thor.say("Function '#{function_name}' doesn't seem to be exists.", :yellow)
    y_or_n = thor.ask("Do you want to create new function '#{function_name}' ? (y/n)")
    if y_or_n =~ /[yY]/
      thor.say("Creating function '#{function_name}' ...", :yellow)
      begin
        function.create_function(config['runtime'], config['role'])
      rescue ArgumentError
        thor.say("ArgumentError occured. You might need to specify role arn \
you want to pass to your function via 'iota.conf' file or ENV['AWS_LAMBDA_IAM_ROLE'].", :red)
        return
      end
      thor.say("Done.", :blue)
      thor.say("Setting aliases.", :yellow)
      first_version = function.publish_version.version
      Iota::Function::ENVIRONMENT_ALIAS_NAME_MAP.values.each do |alias_name|
        function.create_alias(alias_name, first_version)
        function.create_alias(alias_name << '_LAST', first_version)
      end
      thor.say("Done.")
    end
  end
end