faastruby 0.4.18 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -3
  3. data/Gemfile.lock +28 -4
  4. data/README.md +63 -5
  5. data/faastruby.gemspec +5 -1
  6. data/lib/faastruby.rb +1 -0
  7. data/lib/faastruby/api.rb +154 -6
  8. data/lib/faastruby/base.rb +3 -9
  9. data/lib/faastruby/cli.rb +39 -12
  10. data/lib/faastruby/cli/base_command.rb +66 -0
  11. data/lib/faastruby/cli/commands.rb +122 -59
  12. data/lib/faastruby/cli/commands/account/base_command.rb +10 -0
  13. data/lib/faastruby/cli/commands/account/confirm.rb +94 -0
  14. data/lib/faastruby/cli/commands/account/login.rb +86 -0
  15. data/lib/faastruby/cli/commands/account/logout.rb +59 -0
  16. data/lib/faastruby/cli/commands/account/signup.rb +76 -0
  17. data/lib/faastruby/cli/commands/{function.rb → function/base_command.rb} +2 -11
  18. data/lib/faastruby/cli/commands/function/build.rb +18 -11
  19. data/lib/faastruby/cli/commands/function/deploy_to.rb +100 -37
  20. data/lib/faastruby/cli/commands/function/new.rb +89 -36
  21. data/lib/faastruby/cli/commands/function/remove_from.rb +21 -6
  22. data/lib/faastruby/cli/commands/function/run.rb +15 -15
  23. data/lib/faastruby/cli/commands/function/test.rb +5 -4
  24. data/lib/faastruby/cli/commands/function/update_context.rb +10 -3
  25. data/lib/faastruby/cli/commands/function/upgrade.rb +62 -61
  26. data/lib/faastruby/cli/commands/help.rb +33 -20
  27. data/lib/faastruby/cli/commands/project/base_command.rb +14 -0
  28. data/lib/faastruby/cli/commands/project/deploy.rb +114 -0
  29. data/lib/faastruby/cli/commands/project/down.rb +58 -0
  30. data/lib/faastruby/cli/commands/project/new.rb +237 -0
  31. data/lib/faastruby/cli/commands/workspace/cp.rb +107 -0
  32. data/lib/faastruby/cli/commands/workspace/create.rb +35 -27
  33. data/lib/faastruby/cli/commands/workspace/destroy.rb +14 -7
  34. data/lib/faastruby/cli/commands/workspace/list.rb +15 -6
  35. data/lib/faastruby/cli/commands/workspace/migrate.rb +93 -0
  36. data/lib/faastruby/cli/commands/workspace/rm.rb +81 -0
  37. data/lib/faastruby/cli/commands/workspace/update.rb +62 -0
  38. data/lib/faastruby/cli/credentials.rb +58 -57
  39. data/lib/faastruby/cli/new_credentials.rb +63 -0
  40. data/lib/faastruby/cli/package.rb +1 -0
  41. data/lib/faastruby/cli/template.rb +7 -7
  42. data/lib/faastruby/local.rb +188 -0
  43. data/lib/faastruby/local/crystal_runtime.cr +170 -0
  44. data/lib/faastruby/local/functions.rb +7 -0
  45. data/lib/faastruby/local/functions/crystal.rb +64 -0
  46. data/lib/faastruby/local/functions/function.rb +173 -0
  47. data/lib/faastruby/local/functions/ruby.rb +28 -0
  48. data/lib/faastruby/local/listeners.rb +5 -0
  49. data/lib/faastruby/local/listeners/listener.rb +104 -0
  50. data/lib/faastruby/local/logger.rb +37 -0
  51. data/lib/faastruby/local/monkey_patch.rb +38 -0
  52. data/lib/faastruby/local/processors.rb +7 -0
  53. data/lib/faastruby/local/processors/function.rb +151 -0
  54. data/lib/faastruby/local/processors/processor.rb +116 -0
  55. data/lib/faastruby/local/processors/static_file.rb +48 -0
  56. data/lib/faastruby/local/static_files.rb +5 -0
  57. data/lib/faastruby/local/static_files/static_file.rb +59 -0
  58. data/lib/faastruby/server.rb +44 -3
  59. data/lib/faastruby/server/app.rb +107 -0
  60. data/lib/faastruby/server/concurrency_controller.rb +50 -50
  61. data/lib/faastruby/server/config.ru +2 -0
  62. data/lib/faastruby/server/event.rb +3 -0
  63. data/lib/faastruby/server/event_hub.rb +7 -6
  64. data/lib/faastruby/server/local.rb +22 -0
  65. data/lib/faastruby/server/logger.rb +50 -0
  66. data/lib/faastruby/server/project_config.rb +44 -0
  67. data/lib/faastruby/server/puma.rb +4 -0
  68. data/lib/faastruby/server/response.rb +40 -0
  69. data/lib/faastruby/server/runner.rb +116 -21
  70. data/lib/faastruby/server/runner_methods.rb +17 -16
  71. data/lib/faastruby/server/sentinel.rb +496 -0
  72. data/lib/faastruby/supported_runtimes.rb +8 -0
  73. data/lib/faastruby/user.rb +77 -0
  74. data/lib/faastruby/version.rb +1 -1
  75. data/lib/faastruby/workspace.rb +36 -3
  76. data/templates/crystal/example-blank/handler.cr +3 -0
  77. data/templates/crystal/example/spec/handler_spec.cr +11 -6
  78. data/templates/public-web/assets/images/background.png +0 -0
  79. data/templates/public-web/assets/images/ruby.png +0 -0
  80. data/templates/public-web/assets/javascripts/main.js +1 -0
  81. data/templates/public-web/assets/stylesheets/main.css +70 -0
  82. data/templates/public-web/favicon.ico +0 -0
  83. data/templates/ruby/api-404/handler.rb +6 -0
  84. data/templates/ruby/api-root/handler.rb +6 -0
  85. data/templates/ruby/example-blank/handler.rb +0 -23
  86. data/templates/ruby/web-404/404.html +36 -0
  87. data/templates/ruby/web-404/handler.rb +3 -0
  88. data/templates/ruby/web-root/handler.rb +10 -0
  89. data/templates/ruby/web-root/index.html.erb +37 -0
  90. data/templates/ruby/web-root/template.rb +13 -0
  91. metadata +102 -21
  92. data/exe/faastruby-server +0 -76
  93. data/lib/faastruby/cli/commands/credentials.rb +0 -11
  94. data/lib/faastruby/cli/commands/credentials/add.rb +0 -58
  95. data/lib/faastruby/cli/commands/credentials/list.rb +0 -58
  96. data/lib/faastruby/cli/commands/workspace.rb +0 -13
  97. data/lib/faastruby/cli/commands/workspace/deploy.rb +0 -50
  98. data/templates/crystal/example-blank/README.md +0 -22
  99. data/templates/crystal/example-blank/spec/handler_spec.cr +0 -8
  100. data/templates/crystal/example-blank/spec/spec_helper.cr +0 -4
  101. data/templates/crystal/example-blank/src/handler.cr +0 -25
  102. data/templates/ruby/example-blank/Gemfile +0 -7
  103. data/templates/ruby/example-blank/README.md +0 -22
  104. data/templates/ruby/example-blank/spec/handler_spec.rb +0 -16
  105. data/templates/ruby/example-blank/spec/spec_helper.rb +0 -3
@@ -1,22 +1,30 @@
1
1
  module FaaStRuby
2
2
  module Command
3
3
  module Function
4
+ require 'faastruby/cli/commands/function/base_command'
5
+ require 'faastruby/cli/new_credentials'
4
6
  class RemoveFrom < FunctionBaseCommand
5
7
  def initialize(args)
6
8
  @args = args
9
+ help
7
10
  @missing_args = []
8
11
  FaaStRuby::CLI.error(@missing_args, color: nil) if missing_args.any?
9
12
  @workspace_name = @args.shift
10
- load_yaml
11
- @function_name = @yaml_config['name']
12
- FaaStRuby::Credentials.load_for(@workspace_name)
13
13
  parse_options
14
+ load_yaml
15
+ @function_name = @options['function_name'] || @yaml_config['name']
16
+ load_credentials
17
+ end
18
+
19
+ def load_yaml
20
+ return true if @options['function_name']
21
+ super
14
22
  end
15
23
 
16
24
  def run
17
25
  warning unless @options['force']
18
26
  FaaStRuby::CLI.error("Cancelled") unless @options['force'] == 'y'
19
- spinner = spin("Removing function from workspace '#{@workspace_name}'...")
27
+ spinner = spin("Removing function '#{@function_name}' from workspace '#{@workspace_name}'...")
20
28
  workspace = FaaStRuby::Workspace.new(name: @workspace_name)
21
29
  function = FaaStRuby::Function.new(name: @function_name, workspace: workspace)
22
30
  function.destroy
@@ -28,11 +36,16 @@ module FaaStRuby
28
36
  end
29
37
 
30
38
  def self.help
31
- "remove-from".light_cyan + " WORKSPACE_NAME [-y, --yes]"
39
+ "remove-from WORKSPACE_NAME [ARGS]"
32
40
  end
33
41
 
34
42
  def usage
35
- "Usage: faastruby #{self.class.help}"
43
+ puts "\nUsage: faastruby #{self.class.help}"
44
+ puts %(
45
+ -y,--yes # Don't prompt for confirmation
46
+ -f,--function FUNCTION_NAME # Pass the function name instead of attempting
47
+ # to read from the function's config file.
48
+ )
36
49
  end
37
50
 
38
51
  private
@@ -51,6 +64,8 @@ module FaaStRuby
51
64
  case option
52
65
  when '-y', '--yes'
53
66
  @options['force'] = 'y'
67
+ when '--function', '-f'
68
+ @options['function_name'] = @args.shift
54
69
  else
55
70
  FaaStRuby::CLI.error(["Unknown argument: #{option}".red, usage], color: nil)
56
71
  end
@@ -1,9 +1,11 @@
1
1
  module FaaStRuby
2
2
  module Command
3
3
  module Function
4
+ require 'faastruby/cli/commands/function/base_command'
4
5
  class Run < FunctionBaseCommand
5
6
  def initialize(args)
6
7
  @args = args
8
+ help
7
9
  @missing_args = []
8
10
  FaaStRuby::CLI.error(@missing_args, color: nil) if missing_args.any?
9
11
  @options = {}
@@ -31,24 +33,22 @@ module FaaStRuby
31
33
  end
32
34
 
33
35
  def self.help
34
- 'run'.light_cyan + ' WORKSPACE_NAME [OPTIONS]' +
35
- <<-EOS
36
-
37
- Options:
38
- -b, --body 'DATA' # The request body
39
- --stdin # Read the request body from STDIN
40
- -m, --method METHOD # The request method
41
- -h, --header 'Header: Value' # Set a header. Can be used multiple times.
42
- -f, --form 'a=1&b=2' # Send form data and set header 'Content-Type: application/x-www-form-urlencoded'
43
- -j, --json '{"a":"1"}' # Send JSON data and set header 'Content-Type: application/json'
44
- -t, --time # Return function run time in the response
45
- -q, --query 'foo=bar' # Set a query parameter for the request. Can be used multiple times.
46
- --curl # Return the CURL command equivalent for the request
47
- EOS
36
+ 'run WORKSPACE_NAME [ARGS]'
48
37
  end
49
38
 
50
39
  def usage
51
- "Usage: faastruby #{self.class.help}"
40
+ puts "\nUsage: faastruby #{self.class.help}"
41
+ puts %(
42
+ -b,--body 'DATA' # The request body
43
+ --stdin # Read the request body from STDIN
44
+ -m,--method METHOD # The request method
45
+ -h,--header 'Header: Value' # Set a header. Can be used multiple times.
46
+ -f,--form 'a=1&b=2' # Send form data and set header 'Content-Type: application/x-www-form-urlencoded'
47
+ -j,--json '{"a":"1"}' # Send JSON data and set header 'Content-Type: application/json'
48
+ -t,--time # Return function run time in the response
49
+ -q,--query 'foo=bar' # Set a query parameter for the request. Can be used multiple times.
50
+ --curl # Return the CURL command equivalent for the request
51
+ )
52
52
  end
53
53
 
54
54
  private
@@ -2,6 +2,7 @@ require 'open3'
2
2
  module FaaStRuby
3
3
  module Command
4
4
  module Function
5
+ require 'faastruby/cli/commands/function/base_command'
5
6
  class Test < FunctionBaseCommand
6
7
  def initialize(args)
7
8
  @args = args
@@ -12,19 +13,19 @@ module FaaStRuby
12
13
 
13
14
  def run(do_not_exit: false)
14
15
  unless @test_command
15
- puts "[skipped] You have no 'test_command' key/value in 'faastruby.yml'. Please consider using rspec!".yellow
16
+ # puts "[skipped tests] You have no 'test_command' key/value in 'faastruby.yml'. Please consider using rspec!".yellow
16
17
  return true
17
18
  end
18
- puts "[test] Running tests"
19
+ # puts "[test] Running tests"
19
20
  system(@test_command)
20
21
  end
21
22
 
22
23
  def self.help
23
- 'test'.light_cyan
24
+ 'test'
24
25
  end
25
26
 
26
27
  def usage
27
- "Usage: faastruby #{self.class.help}"
28
+ "\nUsage: faastruby #{self.class.help}"
28
29
  end
29
30
  end
30
31
  end
@@ -1,16 +1,19 @@
1
1
  module FaaStRuby
2
2
  module Command
3
3
  module Function
4
+ require 'faastruby/cli/commands/function/base_command'
5
+ require 'faastruby/cli/new_credentials'
4
6
  class UpdateContext < FunctionBaseCommand
5
7
  def initialize(args)
6
8
  @args = args
9
+ help
7
10
  @missing_args = []
8
11
  FaaStRuby::CLI.error(@missing_args, color: nil) if missing_args.any?
9
12
  @workspace_name = @args.shift
10
13
  load_yaml
11
14
  @function_name = @yaml_config['name']
12
- FaaStRuby::Credentials.load_for(@workspace_name)
13
15
  parse_options(require_options: {'data' => 'context data'} )
16
+ load_credentials
14
17
  end
15
18
 
16
19
  def run
@@ -26,11 +29,15 @@ module FaaStRuby
26
29
  end
27
30
 
28
31
  def self.help
29
- "update-context".light_cyan + " WORKSPACE_NAME [-d, --data 'STRING'] [--stdin]"
32
+ "update-context WORKSPACE_NAME [ARGS]"
30
33
  end
31
34
 
32
35
  def usage
33
- "Usage: faastruby #{self.class.help}"
36
+ puts "\nUsage: faastruby #{self.class.help}"
37
+ puts %(
38
+ -d, --data 'STRING' # The context data. Must be quoted.
39
+ --stdin # Read context data from STDIN
40
+ )
34
41
  end
35
42
 
36
43
  private
@@ -1,69 +1,70 @@
1
- module FaaStRuby
2
- module Command
3
- module Function
4
- class Upgrade < FunctionBaseCommand
5
- def initialize(args)
6
- @args = args
7
- end
1
+ # module FaaStRuby
2
+ # module Command
3
+ # module Function
4
+ # require 'faastruby/cli/commands/function/base_command'
5
+ # class Upgrade < FunctionBaseCommand
6
+ # def initialize(args)
7
+ # @args = args
8
+ # end
8
9
 
9
- def run
10
- check_function_name
11
- set_yaml_content
12
- write_yaml
13
- write_gemfile
14
- copy_spec_folder
15
- puts "Upgrade complete. Please read the documentation at https://faastruby.io/tutorial.html to learn about the changes."
16
- puts "To deploy, run 'faastruby deploy-to WORKSPACE_NAME'."
17
- end
10
+ # def run
11
+ # check_function_name
12
+ # set_yaml_content
13
+ # write_yaml
14
+ # write_gemfile
15
+ # copy_spec_folder
16
+ # puts "Upgrade complete. Please read the documentation at https://faastruby.io/tutorial.html to learn about the changes."
17
+ # puts "To deploy, run 'faastruby deploy-to WORKSPACE_NAME'."
18
+ # end
18
19
 
19
- def set_yaml_content
20
- @yaml_content = {
21
- 'name' => @function_name,
22
- 'test_command' => nil,
23
- 'abort_build_when_tests_fail' => false,
24
- 'abort_deploy_when_tests_fail' => false
25
- }
26
- end
20
+ # def set_yaml_content
21
+ # @yaml_content = {
22
+ # 'name' => @function_name,
23
+ # 'test_command' => nil,
24
+ # 'abort_build_when_tests_fail' => false,
25
+ # 'abort_deploy_when_tests_fail' => false
26
+ # }
27
+ # end
27
28
 
28
- def check_function_name
29
- FaaStRuby::CLI.error('You need to run this command from the function directory') unless File.file?('handler.rb')
30
- @function_name = Dir.pwd.split('/').last
31
- unless @function_name
32
- puts "What's the name of this function? (accepts only letters, numbers, - or _)"
33
- print "Enter function name: "
34
- @function_name = STDIN.gets.chomp
35
- end
36
- FaaStRuby::CLI.error('You need to provide a valid function name (letters, numbers, - or _)') unless @function_name.match(/^[a-zA-Z\-_0-9]{1,}$/)
37
- end
29
+ # def check_function_name
30
+ # FaaStRuby::CLI.error('You need to run this command from the function directory') unless File.file?('handler.rb')
31
+ # @function_name = Dir.pwd.split('/').last
32
+ # unless @function_name
33
+ # puts "What's the name of this function? (accepts only letters, numbers, - or _)"
34
+ # print "Enter function name: "
35
+ # @function_name = STDIN.gets.chomp
36
+ # end
37
+ # FaaStRuby::CLI.error('You need to provide a valid function name (letters, numbers, - or _)') unless @function_name.match(/^[a-zA-Z\-_0-9]{1,}$/)
38
+ # end
38
39
 
39
- def write_yaml
40
- write_file("./faastruby.yml", @yaml_content.to_yaml)
41
- end
40
+ # def write_yaml
41
+ # write_file("./faastruby.yml", @yaml_content.to_yaml)
42
+ # end
42
43
 
43
- def write_gemfile
44
- content = "\n# Added by 'faastruby upgrade'\ngroup :test do\n gem 'rspec'\nend\n"
45
- write_file("./Gemfile", content, 'a')
46
- end
44
+ # def write_gemfile
45
+ # content = "\n# Added by 'faastruby upgrade'\ngroup :test do\n gem 'rspec'\nend\n"
46
+ # write_file("./Gemfile", content, 'a')
47
+ # end
47
48
 
48
- def copy_spec_folder
49
- @base_dir = '.'
50
- source = "#{Gem::Specification.find_by_name("faastruby").gem_dir}/example-blank"
51
- FileUtils.cp_r("#{source}/spec", "#{@base_dir}/")
52
- puts "+ d #{@base_dir}/spec".green
53
- puts "+ d #{@base_dir}/spec/helpers".green
54
- puts "+ f #{@base_dir}/spec/helpers/faastruby.rb".green
55
- puts "+ f #{@base_dir}/spec/handler_spec.rb".green
56
- puts "+ f #{@base_dir}/spec/spec_helper.rb".green
57
- end
49
+ # def copy_spec_folder
50
+ # @base_dir = '.'
51
+ # source = "#{Gem::Specification.find_by_name("faastruby").gem_dir}/example-blank"
52
+ # FileUtils.cp_r("#{source}/spec", "#{@base_dir}/")
53
+ # puts "+ d #{@base_dir}/spec".green
54
+ # puts "+ d #{@base_dir}/spec/helpers".green
55
+ # puts "+ f #{@base_dir}/spec/helpers/faastruby.rb".green
56
+ # puts "+ f #{@base_dir}/spec/handler_spec.rb".green
57
+ # puts "+ f #{@base_dir}/spec/spec_helper.rb".green
58
+ # end
58
59
 
59
- def self.help
60
- "upgrade".light_cyan
61
- end
60
+ # def self.help
61
+ # "upgrade"
62
+ # end
62
63
 
63
- def usage
64
- "Usage: faastruby #{self.class.help}"
65
- end
66
- end
67
- end
68
- end
69
- end
64
+ # def usage
65
+ # "Usage: faastruby #{self.class.help}"
66
+ # end
67
+ # end
68
+ # end
69
+ # end
70
+ # end
@@ -1,5 +1,6 @@
1
1
  module FaaStRuby
2
2
  module Command
3
+ require 'faastruby/cli/base_command'
3
4
  class Help < BaseCommand
4
5
  def initialize(args)
5
6
  @args = args
@@ -7,31 +8,43 @@ module FaaStRuby
7
8
 
8
9
  def run
9
10
  puts "FaaStRuby CLI - Manage workspaces and functions hosted at faastruby.io"
11
+ puts "Version: #{FaaStRuby::VERSION}"
10
12
  puts
11
- puts "Usage: faastruby [--region REGION, -h, -v] COMMAND ARGS"
13
+ puts "Usage: faastruby [OPTIONS] COMMAND [--help | -h] [ARGS]"
12
14
  puts
15
+ puts "OPTIONS:"
13
16
  puts 'help, -h, --help # Displays this help'
14
17
  puts '-v # Print version and exit'
15
18
  puts '--region tor1 # Specify a region. "tor1" (default) is the only region available'
16
- workspaces = ["Workspaces:"]
17
- functions = ["Functions:"]
18
- credentials = ["Credentials:"]
19
- FaaStRuby::Command::COMMANDS.each do |command, klass|
20
- next if command == 'upgrade'
21
- next if klass.to_s.match(/.+Command::Help$/)
22
- next if klass.to_s.match(/.+Command::Version$/)
23
- section = functions if klass.to_s.match(/.+::Function::.+/)
24
- section = workspaces if klass.to_s.match(/.+::Workspace::.+/)
25
- section = credentials if klass.to_s.match(/.+::Credentials::.+/)
26
- section ||= []
27
- section << " #{klass.help}"
28
- end
29
- puts workspaces
30
- puts
31
- puts functions
32
- puts
33
- puts credentials
34
- puts
19
+ puts "\nCOMMANDS:"
20
+ puts %(
21
+ Accounts:
22
+ signup
23
+ confirm-account # Send a token over email for account confirmation
24
+ login
25
+ logout
26
+
27
+ Functions:
28
+ new # Initialize a function in your local machine
29
+ deploy-to # Deploy a function to a cloud workspace
30
+ remove-from # Remove a function from a cloud workspace
31
+ run # Trigger the function via HTTP endpoint
32
+ update-context # Update the context data for a function
33
+
34
+ Projects:
35
+ new # Initialize a project in your local machine
36
+ deploy # Deploy all functions and static files of a project
37
+
38
+ Workspaces:
39
+ create-workspace # Create a cloud workspace
40
+ destroy-workspace # Erase a workspace from the cloud
41
+ list-workspace # List what's in a cloud workspace
42
+ cp # Copy a static file from your local machine to a cloud workspace
43
+ rm # Remove a static file from a cloud workspace
44
+ update-workspace # Update workspace settings
45
+
46
+ )
47
+ puts "Run faastruby COMMAND --help for more details."
35
48
  end
36
49
  end
37
50
  end
@@ -0,0 +1,14 @@
1
+ module FaaStRuby
2
+ module Command
3
+ module Project
4
+ PROJECT_YAML_FILE = 'project.yml'
5
+ PROJECT_SECRETS_FILE = 'secrets.yml'
6
+ PROJECT_CREDENTIALS_FILE = '.credentials.yml'
7
+ class ProjectBaseCommand < BaseCommand
8
+ def read_credentials_file
9
+ File.file?(PROJECT_CREDENTIALS_FILE)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,114 @@
1
+ require 'open3'
2
+ module FaaStRuby
3
+ module Command
4
+ STDOUT_MUTEX = Mutex.new
5
+ module Project
6
+ require 'faastruby/cli/commands/project/base_command'
7
+ require 'faastruby/server/logger'
8
+ class Deploy < ProjectBaseCommand
9
+ extend FaaStRuby::Logger
10
+ def initialize(args)
11
+ @errors = []
12
+ @args = args
13
+ help
14
+ parse_options
15
+ @options['functions'] += find_functions unless @options['functions'].any?
16
+ @options['environment'] ||= 'stage'
17
+ @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?")
18
+ @project_name = @project_yaml['name']
19
+ @project_identifier = "-#{@project_yaml['identifier']}" if @project_yaml['identifier']
20
+ @options['root_to'] ||= @project_yaml['root_to']
21
+ @options['catch_all'] ||= @project_yaml['catch_all']
22
+ end
23
+
24
+ def puts(msg)
25
+ STDOUT_MUTEX.synchronize do
26
+ STDOUT.puts msg
27
+ end
28
+ end
29
+
30
+ def run
31
+ result = []
32
+ errors = false
33
+ root_folder = Dir.pwd
34
+ jobs = []
35
+ workspace = "#{@project_name}-#{@options['environment']}#{@project_identifier}"
36
+ try_workspace(workspace)
37
+ spinner = spin("Deploying project '#{@project_name}'...")
38
+ @options['functions'].each do |function_path|
39
+ jobs << Thread.new do
40
+ # puts "[#{function_path}] Entering folder '#{function_path}'"
41
+ # Dir.chdir function_path
42
+ function_config = YAML.load(File.read("#{function_path}/faastruby.yml"))
43
+ function_name = function_config['name']
44
+ cmd = "cd #{function_path} && faastruby deploy-to #{workspace} --quiet"
45
+ cmd += " --set-root" if @options['root_to'] == function_name
46
+ cmd += " --set-catch-all" if @options['catch_all'] == function_name
47
+ Open3.popen2(cmd) do |stdin, stdout, status_thread|
48
+ stdout.each_line do |line|
49
+ puts line
50
+ end
51
+ FaaStRuby::CLI.error("* [#{function_path}] Deploy FAILED", color: nil) unless status_thread.value.success?
52
+ end
53
+ end
54
+ end
55
+ jobs.each{|thr| thr.join}
56
+ spinner.stop(" Done!")
57
+ puts "* Project URL: #{FaaStRuby.workspace_host_for(workspace)}\n\n".green
58
+ end
59
+
60
+ def try_workspace(workspace)
61
+ try_to_create = Proc.new {system("faastruby create-workspace #{workspace} > /dev/null 2>&1")}
62
+ has_credentials = system("faastruby list-workspace #{workspace} > /dev/null 2>&1")
63
+ continue = has_credentials || try_to_create.call
64
+ unless continue
65
+ 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}")
66
+ end
67
+ true
68
+ end
69
+
70
+ def find_functions
71
+ Dir.glob("**/faastruby.yml").map do |f|
72
+ path = f.split('/')
73
+ path.pop
74
+ path.join('/')
75
+ end
76
+ end
77
+
78
+ def self.help
79
+ "deploy [ARGS]"
80
+ end
81
+
82
+ def usage
83
+ puts "Usage: faastruby #{self.class.help}"
84
+ puts %(
85
+ -f,--function FUNCTION_PATH # Specify the path to the function directory in your local machine.
86
+ # This argument can be repeated many times for multiple functions. Example:
87
+ # -f path/to/function1 -f path/to/function2
88
+ -e,--env ENVIRONMENT # ENVIRONMENT is added to the project name to compose the workspace name.
89
+ )
90
+ end
91
+
92
+ def parse_options
93
+ @options = {'functions' => []}
94
+ while @args.any?
95
+ option = @args.shift
96
+ case option
97
+ when '--root-to'
98
+ @options['root_to'] = @args.shift
99
+ when '--catch-all'
100
+ @options['catch_all'] = @args.shift
101
+ when '--function', '-f'
102
+ @options['functions'] << @args.shift
103
+ when '--env', '-e'
104
+ @options['environment'] = @args.shift
105
+ else
106
+ FaaStRuby::CLI.error("Unknown argument: #{option}")
107
+ end
108
+ end
109
+ end
110
+
111
+ end
112
+ end
113
+ end
114
+ end