Class: FaaStRuby::Command::Function::DeployTo

Inherits:
FunctionBaseCommand show all
Defined in:
lib/faastruby/cli/commands/function/deploy_to.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FunctionBaseCommand

#load_yaml

Methods inherited from BaseCommand

#has_user_logged_in?, #help, #load_credentials, #load_yaml, #say, spin, #spin, #write_file

Constructor Details

#initialize(args) ⇒ DeployTo

Returns a new instance of DeployTo.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/faastruby/cli/commands/function/deploy_to.rb', line 7

def initialize(args)
  @args = args
  help
  @missing_args = []
  FaaStRuby::CLI.error(@missing_args, color: nil) if missing_args.any?
  @workspace_name = @args.shift
  parse_options
  load_yaml
  @yaml_config['before_build'] ||= []
  @function_name = @yaml_config['name']
  unless @yaml_config['serve_static']
    @options['root_to'] = @function_name if @options['is_root']
    @options['catch_all'] = @function_name if @options['is_catch_all']
    # @abort_when_tests_fail = true #@yaml_config['abort_deploy_when_tests_fail']
  end
  load_credentials
  @package_file = Tempfile.new('package')
end

Class Method Details

.helpObject



75
76
77
# File 'lib/faastruby/cli/commands/function/deploy_to.rb', line 75

def self.help
  "deploy-to WORKSPACE_NAME [ARGS]"
end

Instance Method Details

#crystal_runtime?Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/faastruby/cli/commands/function/deploy_to.rb', line 30

def crystal_runtime?
  return false if @yaml_config['runtime'].nil?
  @yaml_config['runtime'].match(/^crystal/)
end

#ruby_runtime?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/faastruby/cli/commands/function/deploy_to.rb', line 26

def ruby_runtime?
  @yaml_config['runtime'].nil? || @yaml_config['runtime'].match(/^ruby/)
end

#runObject



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
# File 'lib/faastruby/cli/commands/function/deploy_to.rb', line 41

def run
  create_or_use_workspace
  if @yaml_config['serve_static']
    package_file_name = build_package
    spinner = say("[#{@function_name}] Deploying static files '#{@function_name}' to workspace '#{@workspace_name}'...", quiet: @options['quiet'])
    workspace = FaaStRuby::Workspace.new(name: @workspace_name).deploy(package_file_name)
  else
    if ruby_runtime?
      FaaStRuby::CLI.error('Please fix the problems above and try again') unless bundle_install
    end
    if crystal_runtime?
      FaaStRuby::CLI.error('Please fix the problems above and try again') unless shards_install
    end
    FaaStRuby::CLI.error("[#{@function_name}] Deploy aborted because 'test_command' exited non-zero.") unless run_tests
    package_file_name = build_package
    spinner = say("[#{@function_name}] Deploying #{runtime_name} function '#{@function_name}' to workspace '#{@workspace_name}'...", quiet: @options['quiet'])
    workspace = FaaStRuby::Workspace.new(name: @workspace_name).deploy(package_file_name, root_to: @options['root_to'], catch_all: @options['catch_all'], context: @options['context'])
  end
  if workspace.errors.any?
    puts ' Failed :(' unless spinner&.stop(' Failed :(')
    @package_file.unlink
    FaaStRuby::CLI.error(workspace.errors)
  end
  spinner.stop(' Done!') unless @options['quiet']
  @package_file.unlink
  puts "* [#{@function_name}] Deploy OK".green
  unless @yaml_config['serve_static']
    puts "* [#{@function_name}] Workspace: #{@workspace_name}".green
    puts "* [#{@function_name}] Endpoint: #{FaaStRuby.workspace_host_for(@workspace_name)}/#{@function_name unless @options['root_to']}".green
  end
  puts '---'
  exit 0
end

#runtime_nameObject



35
36
37
38
39
# File 'lib/faastruby/cli/commands/function/deploy_to.rb', line 35

def runtime_name
  return 'Ruby' if ruby_runtime?
  return 'Crystal' if crystal_runtime?
  return 'Ruby'
end

#usageObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/faastruby/cli/commands/function/deploy_to.rb', line 79

def usage
  puts "\nUsage: faastruby #{self.class.help}"
  puts %(
-f,--function PATH/TO/FUNCTION     # Specify the directory where the function is.
--context DATA                     # The data to be stored as context in the cloud,
                           # accessible via 'event.context' from within your function.
--set-root                         # Set the function as the root route of the workspace/
--set-catch-all                    # Set the function as the catch-all route of the workspace.
--dont-create-workspace            # Don't try to create the workspace if it doesn't exist.
  )
end