Class: Lamma::CLI::Init

Inherits:
Object
  • Object
show all
Includes:
SharedHelpers, Thor::Actions
Defined in:
lib/lamma/cli/init.rb

Constant Summary collapse

ROLE_ARN_PLACEHOLDER =
'YOUR_ROLE_ARN'
DEFAULT_LAMBDA_ASSUME_ROLE_POLICY =
{
  'Version': '2012-10-17',
  'Statement':
  [
    {
      'Effect': 'Allow',
      'Principal':
      {
        'Service': 'lambda.amazonaws.com'
      },
      'Action': 'sts:AssumeRole',
    },
  ],
}

Constants included from SharedHelpers

SharedHelpers::DEFAULT_PROFILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SharedHelpers

#ini_config, #ini_credentials, #region_or_raise, #search_conf_path

Constructor Details

#initialize(options, function_name, thor) ⇒ Init

Returns a new instance of Init.



36
37
38
39
40
41
42
43
# File 'lib/lamma/cli/init.rb', line 36

def initialize(options, function_name, thor)
  @options = options
  @thor = thor
  runtime_str = options.fetch('runtime') { raise ArgumentError.new('runtime must be set.') }
  @runtime = Lamma::Runtime.new(runtime_str)
  @function_name = function_name
  @target = Pathname.pwd.join(function_name)
end

Instance Attribute Details

#function_nameObject (readonly)

Returns the value of attribute function_name.



34
35
36
# File 'lib/lamma/cli/init.rb', line 34

def function_name
  @function_name
end

#optionsObject (readonly)

Returns the value of attribute options.



34
35
36
# File 'lib/lamma/cli/init.rb', line 34

def options
  @options
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



34
35
36
# File 'lib/lamma/cli/init.rb', line 34

def runtime
  @runtime
end

#targetObject (readonly)

Returns the value of attribute target.



34
35
36
# File 'lib/lamma/cli/init.rb', line 34

def target
  @target
end

#thorObject (readonly)

Returns the value of attribute thor.



34
35
36
# File 'lib/lamma/cli/init.rb', line 34

def thor
  @thor
end

Instance Method Details

#runObject



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
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/lamma/cli/init.rb', line 45

def run
  if Dir.exist?(target)
    Lamma.logger.fatal "Directory '#{target}' already exists."
    exit(1)
  end

  tpath = File.join(File.dirname(__FILE__), '..', 'templates', runtime.to_dirname)
  p tpath
  templates = Dir.glob(File.join(tpath, '**/*.erb')).map do |path|
    tar_file = path[tpath.length..path.length - 5] # /foo/bar/templates/RUNTIME/baz/lambda_function.py.erb => /baz/lambda_function.py

    [File.expand_path(path), File.join(target, tar_file)]
  end.to_h

  unless Dir.exist?(target)
    ::FileUtils.makedirs(target)
  end

  role_arn = options['role_arn']

  unless role_arn
    role_name = "#{function_name}-lamma-role"
    thor.say("Looks like you didn't specified role arn for the function.", :yellow)
    y_or_n = thor.ask("Do you want me to create default IAM role and configure it (#{role_name})? (y/n)", :yellow)

    if y_or_n =~ /^[yY]/
      role_arn = create_initial_role(role_name)
    else
      role_arn = 'YOUR_ROLE_ARN'
    end
  end

  templates.each do |src, dst|
    thor.say_status(:create, dst)

    File.open(dst, "w") do |f|
      template = File.read(src)
      formatted = ERB.new(template).result(binding)
      f.write(formatted)
    end
  end


  unless options['skip_git']
    p options
    Lamma.logger.info "Initializing git repo in #{target}"
    Dir.chdir(target) do
      `git init`
    end
  end
end