Class: FaaStRuby::Command::Project::Deploy

Inherits:
ProjectBaseCommand show all
Extended by:
Logger
Defined in:
lib/faastruby/cli/commands/project/deploy.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ProjectBaseCommand

#read_credentials_file

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.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 10

def initialize(args)
  @errors = []
  @args = args
  help
  parse_options
  @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_name = @project_yaml['name']
  @project_identifier = "-#{@project_yaml['identifier']}" if @project_yaml['identifier']
  @options['root_to'] ||= @project_yaml['root_to']
  @options['catch_all'] ||= @project_yaml['catch_all']
end

Class Method Details

.helpObject



78
79
80
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 78

def self.help
  "deploy [ARGS]"
end

Instance Method Details

#find_functionsObject



70
71
72
73
74
75
76
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 70

def find_functions
  Dir.glob("**/faastruby.yml").map do |f|
    path = f.split('/')
    path.pop
    path.join('/')
  end
end

#parse_optionsObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 92

def parse_options
  @options = {'functions' => []}
  while @args.any?
    option = @args.shift
    case option
    when '--root-to'
      @options['root_to'] = @args.shift
    when '--catch-all'
      @options['catch_all'] = @args.shift
    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



24
25
26
27
28
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 24

def puts(msg)
  STDOUT_MUTEX.synchronize do
    STDOUT.puts msg
  end
end

#runObject



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
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 30

def run
  result = []
  errors = false
  root_folder = Dir.pwd
  jobs = []
  workspace = "#{@project_name}-#{@options['environment']}#{@project_identifier}"
  try_workspace(workspace)
  spinner = spin("Deploying project '#{@project_name}'...")
  @options['functions'].each do |function_path|
    jobs << Thread.new do
      # puts "[#{function_path}] Entering folder '#{function_path}'"
      # Dir.chdir function_path
      function_config = YAML.load(File.read("#{function_path}/faastruby.yml"))
      function_name = function_config['name']
      cmd = "cd #{function_path} && faastruby deploy-to #{workspace} --quiet"
      cmd += " --set-root" if @options['root_to'] == function_name
      cmd += " --set-catch-all" if @options['catch_all'] == function_name
      Open3.popen2(cmd) do |stdin, stdout, status_thread|
        stdout.each_line do |line|
          puts line
        end
        FaaStRuby::CLI.error("* [#{function_path}] Deploy FAILED", color: nil) unless status_thread.value.success?
      end
    end
  end
  jobs.each{|thr| thr.join}
  spinner.stop(" Done!")
  puts "* Project URL: #{FaaStRuby.workspace_host_for(workspace)}\n\n".green
end

#try_workspace(workspace) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 60

def try_workspace(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
    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 --deploy-env #{@options['environment']}-#{(rand * 100).to_i}")
  end
  true
end

#usageObject



82
83
84
85
86
87
88
89
90
# File 'lib/faastruby/cli/commands/project/deploy.rb', line 82

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 name to compose the workspace name.
  )
end