Class: FaaStRuby::Command::Project::Deploy
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from BaseCommand
#has_user_logged_in?, #help, #load_credentials, #load_yaml, #say, spin, #spin, #write_file
Constructor Details
#initialize(args) ⇒ Deploy
Returns a new instance of Deploy.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 11
def initialize(args)
@errors = []
@args = args
help
parse_options
@mutex = Mutex.new
@failed = {}
@options['functions'] += find_functions unless @options['functions'].any?
@options['environment'] ||= 'stage'
@project_yaml = YAML.load(File.read(PROJECT_YAML_FILE))['project'] rescue FaaStRuby::CLI.error("Could not find file 'project.yml'. Are you running this command from the project's folder?")
@project_secrets = (YAML.load(File.read(PROJECT_SECRETS_FILE))['secrets'] || {}) rescue {}
@project_name = @project_yaml['name']
@root_to = @project_yaml['root_to'] || 'root'
@catch_all = @project_yaml['catch_all'] || 'catch-all'
@project_identifier = "-#{@project_yaml['identifier']}" if @project_yaml['identifier']
@workspace = "#{@project_name}-#{@options['environment']}#{@project_identifier}"
@spinners = TTY::Spinner::Multi.new("Deploying project '#{@project_name}' to workspace '#{@workspace}'", format: SPINNER_FORMAT)
end
|
Class Method Details
.help ⇒ Object
117
118
119
|
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 117
def self.help
"deploy [ARGS]"
end
|
Instance Method Details
#add_failed(function_name, output) ⇒ Object
90
91
92
93
94
|
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 90
def add_failed(function_name, output)
@mutex.synchronize do
@failed[function_name] = output
end
end
|
#find_functions ⇒ Object
109
110
111
112
113
114
115
|
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 109
def find_functions
Dir.glob("**/faastruby.yml").map do |f|
path = f.split('/')
path.pop
path.join('/')
end
end
|
#parse_options ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 133
def parse_options
@options = {'functions' => []}
while @args.any?
option = @args.shift
case option
when '--skip-dependencies'
@options['skip_dependencies'] = true
when '--skip-create-workspace'
@options['skip_create_workspace'] = true
when '--function', '-f'
@options['functions'] << @args.shift
when '--env', '-e'
@options['environment'] = @args.shift
else
FaaStRuby::CLI.error("Unknown argument: #{option}")
end
end
end
|
#puts(msg) ⇒ Object
31
32
33
34
35
|
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 31
def puts(msg)
STDOUT_MUTEX.synchronize do
STDOUT.puts msg
end
end
|
#run ⇒ Object
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/faastruby/cli/commands/project/deploy.rb', line 37
def run
result = []
errors = false
root_folder = Dir.pwd
jobs = []
connect_spinner = @spinners.register "[:spinner] Connecting to workspace '#{@workspace}'"
connect_spinner.auto_spin
try_workspace(@workspace, connect_spinner)
@options['functions'].each do |function_path|
jobs << Thread.new do
function_config = YAML.load(File.read("#{function_path}/faastruby.yml"))
function_name = function_config['name']
msg = function_name == 'public' ? "Uploading static assets in '#{function_name}'" : "Deploying function from '#{function_path}'"
spinner = @spinners.register "[:spinner] #{msg}"
spinner.auto_spin
cmd = "cd #{function_path} && faastruby deploy-to #{@workspace} --quiet --dont-create-workspace #{'--skip-dependencies' if @options['skip_dependencies']}"
cmd += " --set-root" if @root_to == function_name
cmd += " --set-catch-all" if @catch_all == function_name
secrets = secrets_for(function_name)
secrets_json = Oj.dump(secrets) if secrets
cmd += " --context '#{secrets_json}'" if secrets_json
output, status = Open3.capture2e(cmd)
if status == 0
spinner.success
else
add_failed(function_name, output)
spinner.error
end
end
end
jobs.each{|thr| thr.join}
if @failed.any?
puts "\n\nFAILURES:".red
@failed.each do |function_name, output|
puts "* Function '#{function_name}' deploy failed:".red
puts output
puts nil
end
else
puts "* Project URL: #{FaaStRuby.workspace_host_for(@workspace)}\n".green
end
end
|
#secrets_for(function_name) ⇒ Object
84
85
86
87
88
|
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 84
def secrets_for(function_name)
secrets = @project_secrets[@options['environment']]
return nil unless secrets
secrets[function_name]
end
|
#try_workspace(workspace, connect_spinner) ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 96
def try_workspace(workspace, connect_spinner)
return true if @options['skip_create_workspace']
try_to_create = Proc.new {system("faastruby create-workspace #{workspace} > /dev/null 2>&1")}
has_credentials = system("faastruby list-workspace #{workspace} > /dev/null 2>&1")
continue = has_credentials || try_to_create.call
unless continue
connect_spinner.error
FaaStRuby::CLI.error("Unable to deploy project to workspace '#{workspace}'. Make sure you have the credentials, or try a different environment name.\nExample: faastruby deploy --env #{@options['environment']}-#{(rand * 100).to_i}")
end
connect_spinner.success
true
end
|
#usage ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 121
def usage
puts "Usage: faastruby #{self.class.help}"
puts %(
-f,--function FUNCTION_PATH # Specify the path to the function directory in your local machine.
# This argument can be repeated many times for multiple functions.
# Example: -f path/to/function1 -f path/to/function2
-e,--env ENVIRONMENT # ENVIRONMENT is added to the project's name to compose the workspace name.
--skip-dependencies # Don't try to install Gems or Shards before creating
# the deployment package
)
end
|