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
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
151
152
153
154
|
# File 'lib/jarbs.rb', line 30
def run
program :version, Jarbs::VERSION
program :description, 'Lambda Tooling'
global_option('-e', '--env [dev]', String, 'Set deployment environment')
global_option('-d', '--debug', 'Enable debug mode') { $debug = true }
global_option('-p', '--profile [default]', String, 'AWS credential profile to use') do |profile|
@config.set('aws.profile', profile)
end
command :init do |c|
c.syntax = 'jarbs init'
c.summary = 'Setup lambda project in an existing directory'
c.description = "Git ignores jarbs project definition file so on checkout of an existing lambda function.\nWhen checking out a project from source control or transitioning a legacy lambda codebase to use\nwith jarbs, you'll need to run this command to setup the initial config and run additional checks\nfor compatability.\n DESC\n c.action do |args, options|\n skip_setup = File.exists? '.jarbs'\n abort('Lambda project already initialized.') if skip_setup\n\n unless Dir.exists? 'lambdas'\n say_warning(\"This doesn't look like a jarbs-enabled project directory (missing lambdas subdir).\")\n skip_setup = !agree('Continue (y/n)? ')\n end\n\n Config.touch unless skip_setup\n end\n end\n\n command :new do |c|\n c.syntax = 'jarbs new [options] name'\n c.summary = \"Generate a new lambda function or project skeleton\"\n c.option \"-f\", \"--force\", \"Force overwrite of existing function definition\"\n c.action do |args, options|\n name = args.shift || abort(\"Must provide a lambda name\")\n options.default global_defaults\n\n lambda = Lambda.new(name, options)\n\n project_exists?(name, remove: options.force)\n lambda_exists?(lambda, remove: options.force)\n\n ProjectGenerator.new(name).generate unless jarbs_project?\n lambda.generate\n end\n end\n\n command :deploy do |c|\n c.syntax = 'jarbs deploy [options] directory'\n c.summary = 'Deploy a lambda function to AWS'\n c.option '--role [STRING]', String, 'IAM role for Lambda execution'\n c.option '--dry', 'Dry run (do not interact with AWS)'\n c.action do |args, options|\n name = args.shift || abort('Name argument required')\n options.default global_defaults\n\n lambda = Lambda.new(name, options)\n abort(\"Lambda '\#{name}' does not exist.\") unless lambda.exists?\n\n lambda.prepare\n\n if options.dry\n say_warning('Dry run: Did not deploy to lambda.')\n else\n lambda.deploy\n end\n end\n end\n\n command :rm do |c|\n c.syntax = 'jarbs rm NAME [NAME...]'\n c.summary = \"Delete a lambda function\"\n c.action do |args, options|\n abort('Name argument required') if args.empty?\n options.default global_defaults\n\n begin\n args.each do |fn|\n begin\n Lambda.new(fn, options).delete\n rescue Aws::Lambda::Errors::ResourceNotFoundException => e\n say_error \"Function \\\"\#{fn}\\\" does not exists. Ignoring.\"\n next\n end\n end\n end\n end\n end\n\n command :ls do |c|\n c.syntax = 'jarbs ls'\n c.summary = \"List lambda functions in this project\"\n c.action do |args, options|\n lambdas = Dir.glob(\"lambdas/*\").map {|x| Lambda.new(File.basename(x), options) }\n\n lambdas.each do |l|\n say \"\#{l.function.name}: \#{l.function.description}\"\n end\n end\n end\n\n command :invoke do |c|\n c.syntax = 'jarbs run NAME [payload]'\n c.summary = 'Invoke the lambda function and prints the cloudwatch logs.'\n c.option '--file FILE', 'JSON file to use as the payload (ignored if payload is specified in the command).'\n c.action do |args, options|\n name = args.shift || abort('Name argument required')\n payload = args.shift || \"\"\n\n if payload.nil? && File.exists?(options.file)\n payload = File.read(options.file)\n end\n\n options.default global_defaults\n\n lambda = Lambda.new(name, options)\n lambda.invoke(payload)\n end\n end\n\n run!\nend\n"
|