faastruby 0.5.8 → 0.5.9
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/faastruby/cli/commands/function/deploy_to.rb +6 -6
- data/lib/faastruby/local.rb +1 -0
- data/lib/faastruby/local/functions/function.rb +11 -12
- data/lib/faastruby/local/functions/ruby.rb +10 -0
- data/lib/faastruby/local/listeners/listener.rb +7 -1
- data/lib/faastruby/local/processors/function.rb +24 -2
- data/lib/faastruby/local/static_files/static_file.rb +11 -8
- data/lib/faastruby/server/app.rb +1 -0
- data/lib/faastruby/version.rb +1 -1
- metadata +2 -3
- data/lib/faastruby/server/sentinel.rb +0 -496
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: ad8d16f69c2faa2730dde9c389f3d2485bbbba81b348ae20bd5f4e6074cd4477
         | 
| 4 | 
            +
              data.tar.gz: 0b156a957b0cb34b436353cf0e89f192232b8f01239ec4f41c5ef9e3bb52c97d
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 3292a49454d2a445d032653569ef201f2365c676ffa7b82c907ba4e3edda2e62043c58e2b2493c0747c96131df94eef1a2d58ae0acbff791c2abdd77881cb981
         | 
| 7 | 
            +
              data.tar.gz: b78fdfb6a57e1666579f8ab0330bb4c2cce3872dd18ec1ff41ee69e9f43c09c3ecfe936151bacd15eecae42293c21fca3b03f1c60056b140d2b64a818c617dbe
         | 
    
        data/CHANGELOG.md
    CHANGED
    
    | @@ -1,5 +1,10 @@ | |
| 1 1 | 
             
            # Changelog
         | 
| 2 2 |  | 
| 3 | 
            +
            ## 0.5.9 - Mar 11 2019
         | 
| 4 | 
            +
            - Add Gemfile support to Watchdog. When a `Gemfile` is added to a function, Watchdog will initialize it if the file is empty and run `bundle install` every time you modify it.
         | 
| 5 | 
            +
            - Watchdog will stream the output of commands instead of printing everything once it's done.
         | 
| 6 | 
            +
            - Disable caching for static files on Local
         | 
| 7 | 
            +
             | 
| 3 8 | 
             
            ## 0.5.8 - Mar 11 2019
         | 
| 4 9 | 
             
            - Bump dependency faastruby-rpc to v0.2.3
         | 
| 5 10 | 
             
            - Use CSS gradient instead of background picture on Local Web template
         | 
| @@ -42,7 +42,7 @@ module FaaStRuby | |
| 42 42 | 
             
                      create_or_use_workspace
         | 
| 43 43 | 
             
                      if @yaml_config['serve_static']
         | 
| 44 44 | 
             
                        package_file_name = build_package
         | 
| 45 | 
            -
                        spinner = say("[#{@function_name}] Deploying static files '#{@function_name}' to workspace '#{@workspace_name}'...", quiet: @options['quiet'])
         | 
| 45 | 
            +
                        spinner = say("* [#{@function_name}] Deploying static files '#{@function_name}' to workspace '#{@workspace_name}'...", quiet: @options['quiet'])
         | 
| 46 46 | 
             
                        workspace = FaaStRuby::Workspace.new(name: @workspace_name).deploy(package_file_name)
         | 
| 47 47 | 
             
                      else
         | 
| 48 48 | 
             
                        if ruby_runtime?
         | 
| @@ -53,7 +53,7 @@ module FaaStRuby | |
| 53 53 | 
             
                        end
         | 
| 54 54 | 
             
                        FaaStRuby::CLI.error("[#{@function_name}] Deploy aborted because 'test_command' exited non-zero.") unless run_tests
         | 
| 55 55 | 
             
                        package_file_name = build_package
         | 
| 56 | 
            -
                        spinner = say("[#{@function_name}] Deploying #{runtime_name} function '#{@function_name}' to workspace '#{@workspace_name}'...", quiet: @options['quiet'])
         | 
| 56 | 
            +
                        spinner = say("* [#{@function_name}] Deploying #{runtime_name} function '#{@function_name}' to workspace '#{@workspace_name}'...", quiet: @options['quiet'])
         | 
| 57 57 | 
             
                        workspace = FaaStRuby::Workspace.new(name: @workspace_name).deploy(package_file_name, root_to: @options['root_to'], catch_all: @options['catch_all'], context: @options['context'])
         | 
| 58 58 | 
             
                      end
         | 
| 59 59 | 
             
                      if workspace.errors.any?
         | 
| @@ -107,14 +107,14 @@ module FaaStRuby | |
| 107 107 |  | 
| 108 108 | 
             
                    def shards_install
         | 
| 109 109 | 
             
                      return true unless File.file?('shard.yml')
         | 
| 110 | 
            -
                      puts "[#{@function_name}]  | 
| 111 | 
            -
                      system('shards check') || system('shards install')
         | 
| 110 | 
            +
                      puts "[#{@function_name}] Verifying dependencies"
         | 
| 111 | 
            +
                      system('shards check > /dev/null') || system('shards install')
         | 
| 112 112 | 
             
                    end
         | 
| 113 113 |  | 
| 114 114 | 
             
                    def bundle_install
         | 
| 115 115 | 
             
                      return true unless File.file?('Gemfile')
         | 
| 116 | 
            -
                      puts "[#{@function_name}]  | 
| 117 | 
            -
                      system('bundle check') || system('bundle install')
         | 
| 116 | 
            +
                      puts "* [#{@function_name}] Verifying dependencies"
         | 
| 117 | 
            +
                      system('bundle check >/dev/null') || system('bundle install')
         | 
| 118 118 | 
             
                    end
         | 
| 119 119 |  | 
| 120 120 | 
             
                    def missing_args
         | 
    
        data/lib/faastruby/local.rb
    CHANGED
    
    
| @@ -63,18 +63,17 @@ module FaaStRuby | |
| 63 63 | 
             
                  def deploy
         | 
| 64 64 | 
             
                    debug "deploy"
         | 
| 65 65 | 
             
                    deploy_cmd, deploy_cmd_print = generate_deploy_command
         | 
| 66 | 
            -
                    puts "Running: #{deploy_cmd_print.join(' ')}"
         | 
| 67 | 
            -
                     | 
| 68 | 
            -
                     | 
| 69 | 
            -
                     | 
| 70 | 
            -
             | 
| 71 | 
            -
             | 
| 72 | 
            -
                       | 
| 73 | 
            -
                    else
         | 
| 74 | 
            -
                      puts "* [#{name}] Deploy Failed:"
         | 
| 75 | 
            -
                      STDERR.puts output
         | 
| 66 | 
            +
                    puts "Running: #{deploy_cmd_print.join(' ')}".yellow
         | 
| 67 | 
            +
                    i, oe, thr = Open3.popen2(deploy_cmd.join(' '))
         | 
| 68 | 
            +
                    i.close
         | 
| 69 | 
            +
                    oe.each_line do |line|
         | 
| 70 | 
            +
                      next if line.chomp == '' || line.chomp == '---'
         | 
| 71 | 
            +
                      STDOUT.puts "#{Time.now} | #{line}"
         | 
| 72 | 
            +
                      STDOUT.puts "---"
         | 
| 76 73 | 
             
                    end
         | 
| 77 | 
            -
                     | 
| 74 | 
            +
                    thr.join
         | 
| 75 | 
            +
                    oe.close
         | 
| 76 | 
            +
                    status = thr.value
         | 
| 78 77 | 
             
                  end
         | 
| 79 78 |  | 
| 80 79 | 
             
                  def language
         | 
| @@ -89,7 +88,7 @@ module FaaStRuby | |
| 89 88 | 
             
                  def generate_deploy_command
         | 
| 90 89 | 
             
                    debug "generate_deploy_command"
         | 
| 91 90 | 
             
                    project_config = Local.project_config
         | 
| 92 | 
            -
                    deploy_cmd = ['faastruby', 'deploy-to', Local.workspace, '-f', @absolute_folder, '--dont-create-workspace']
         | 
| 91 | 
            +
                    deploy_cmd = ['faastruby', 'deploy-to', Local.workspace, '-f', @absolute_folder, '--dont-create-workspace', '--quiet']
         | 
| 93 92 | 
             
                    deploy_cmd << '--set-root' if Local.root_to == @name
         | 
| 94 93 | 
             
                    deploy_cmd << '--set-catch-all' if Local.catch_all == @name
         | 
| 95 94 | 
             
                    secrets_json = Oj.dump(Local.secrets_for_function(@name)) rescue nil
         | 
| @@ -3,6 +3,16 @@ module FaaStRuby | |
| 3 3 | 
             
                class RubyFunction < Function
         | 
| 4 4 | 
             
                  include Local::Logger
         | 
| 5 5 |  | 
| 6 | 
            +
                  def self.default_gemfile
         | 
| 7 | 
            +
                    faastruby_rpc_version = Gem::DependencyList.from_specs.select{|d| d.name == 'faastruby-rpc'}[0]&.version || '0.2.3'
         | 
| 8 | 
            +
                    %(source 'https://rubygems.org'
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            group :test do
         | 
| 11 | 
            +
              gem 'rspec'
         | 
| 12 | 
            +
            end
         | 
| 13 | 
            +
            )
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
             | 
| 6 16 | 
             
                  def yaml_hash
         | 
| 7 17 | 
             
                    debug "yaml_hash"
         | 
| 8 18 | 
             
                    hash = {
         | 
| @@ -45,12 +45,13 @@ module FaaStRuby | |
| 45 45 |  | 
| 46 46 | 
             
                class ListenerEvent
         | 
| 47 47 | 
             
                  include Local::Logger
         | 
| 48 | 
            -
                  attr_accessor :type, :filename, :full_path, :relative_path, :listened_directory, :dirname
         | 
| 48 | 
            +
                  attr_accessor :type, :filename, :full_path, :relative_path, :relative_path_dirname, :listened_directory, :dirname
         | 
| 49 49 | 
             
                  def initialize(type:, full_path:, listened_directory:)
         | 
| 50 50 | 
             
                    debug "initialize(type: #{type.inspect}, full_path: #{full_path.inspect}, listened_directory: #{listened_directory.inspect})"
         | 
| 51 51 | 
             
                    @listened_directory = listened_directory
         | 
| 52 52 | 
             
                    @full_path = full_path
         | 
| 53 53 | 
             
                    @relative_path = relative_path_for(@full_path.dup)
         | 
| 54 | 
            +
                    @relative_path_dirname = File.dirname(@relative_path)
         | 
| 54 55 | 
             
                    @filename = File.basename(@full_path)
         | 
| 55 56 | 
             
                    @dirname = File.dirname(@full_path)
         | 
| 56 57 | 
             
                    @type = type
         | 
| @@ -64,6 +65,11 @@ module FaaStRuby | |
| 64 65 | 
             
                    added? && filename.match(/^handler\.(rb|cr)$/)
         | 
| 65 66 | 
             
                  end
         | 
| 66 67 |  | 
| 68 | 
            +
                  def file_is_a_gemfile?
         | 
| 69 | 
            +
                    debug __method__
         | 
| 70 | 
            +
                    filename == 'Gemfile'
         | 
| 71 | 
            +
                  end
         | 
| 72 | 
            +
             | 
| 67 73 | 
             
                  def file_is_a_handler?
         | 
| 68 74 | 
             
                    debug __method__
         | 
| 69 75 | 
             
                    filename.match(/^handler\.(rb|cr)$/)
         | 
| @@ -12,6 +12,7 @@ module FaaStRuby | |
| 12 12 | 
             
                      debug "ignoring #{event.filename}"
         | 
| 13 13 | 
             
                      return true
         | 
| 14 14 | 
             
                    end
         | 
| 15 | 
            +
                    return true if event.filename == 'Gemfile.lock'
         | 
| 15 16 | 
             
                    return false
         | 
| 16 17 | 
             
                  end
         | 
| 17 18 |  | 
| @@ -34,7 +35,8 @@ module FaaStRuby | |
| 34 35 | 
             
                      debug "added: a handler file was added"
         | 
| 35 36 | 
             
                      return new_function(event)
         | 
| 36 37 | 
             
                    end
         | 
| 37 | 
            -
                     | 
| 38 | 
            +
                    init_gemfile(event) if event.file_is_a_gemfile?
         | 
| 39 | 
            +
                    unless event.file_is_a_function_config? && event.file_is_a_gemfile?
         | 
| 38 40 | 
             
                      debug "added: a file was added"
         | 
| 39 41 | 
             
                      deploy(event)
         | 
| 40 42 | 
             
                    end
         | 
| @@ -45,6 +47,7 @@ module FaaStRuby | |
| 45 47 | 
             
                    # This should trigger
         | 
| 46 48 | 
             
                    # - Compile
         | 
| 47 49 | 
             
                    # - Deploy
         | 
| 50 | 
            +
                    bundle_install(event) if event.file_is_a_gemfile?
         | 
| 48 51 | 
             
                    compile_function(event)
         | 
| 49 52 | 
             
                    deploy(event)
         | 
| 50 53 | 
             
                  end
         | 
| @@ -96,7 +99,7 @@ module FaaStRuby | |
| 96 99 | 
             
                    object = function_object_for_handler(event.filename)
         | 
| 97 100 | 
             
                    function = object.new(
         | 
| 98 101 | 
             
                      absolute_folder: event.dirname,
         | 
| 99 | 
            -
                      name:  | 
| 102 | 
            +
                      name: event.relative_path_dirname,
         | 
| 100 103 | 
             
                    )
         | 
| 101 104 | 
             
                    run(function.name, 'new_function') do
         | 
| 102 105 | 
             
                      debug "+ IGNORE #{event.dirname}"
         | 
| @@ -146,6 +149,25 @@ module FaaStRuby | |
| 146 149 | 
             
                    run(function.name, 'remove_from_workspace') {function.remove_from_workspace}
         | 
| 147 150 | 
             
                  end
         | 
| 148 151 |  | 
| 152 | 
            +
                  def bundle_install(event)
         | 
| 153 | 
            +
                    puts "Running: cd #{event.relative_path_dirname} && bundle install"
         | 
| 154 | 
            +
                    if system("cd #{event.dirname} && bundle install")
         | 
| 155 | 
            +
                      STDOUT.puts '---'.yellow
         | 
| 156 | 
            +
                      puts "Gems from Gemfile '#{event.relative_path}' installed."
         | 
| 157 | 
            +
                    else
         | 
| 158 | 
            +
                      STDOUT.puts '---'.red
         | 
| 159 | 
            +
                      STDOUT.puts "#{Time.now} | Error installing gems for Gemfile '#{event.relative_path}'.".red
         | 
| 160 | 
            +
                    end
         | 
| 161 | 
            +
                  end
         | 
| 162 | 
            +
             | 
| 163 | 
            +
                  def init_gemfile(event)
         | 
| 164 | 
            +
                    unless File.size(event.full_path) > 0
         | 
| 165 | 
            +
                      puts "Initializing Gemfile '#{event.relative_path}'"
         | 
| 166 | 
            +
                      sleep 0.2
         | 
| 167 | 
            +
                      File.write(event.full_path, Local::RubyFunction.default_gemfile)
         | 
| 168 | 
            +
                    end
         | 
| 169 | 
            +
                  end
         | 
| 170 | 
            +
             | 
| 149 171 | 
             
                end
         | 
| 150 172 | 
             
              end
         | 
| 151 173 | 
             
            end
         | 
| @@ -29,15 +29,18 @@ module FaaStRuby | |
| 29 29 | 
             
                    path = "public/#{@relative_path}"
         | 
| 30 30 | 
             
                    cmd = "faastruby cp '#{path}' '#{Local.workspace}:/#{@relative_path}'"
         | 
| 31 31 | 
             
                    puts "Running: #{cmd}"
         | 
| 32 | 
            -
                     | 
| 33 | 
            -
                     | 
| 34 | 
            -
                     | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
                      puts " | 
| 38 | 
            -
                       | 
| 32 | 
            +
                    i, oe, thr = Open3.popen2(cmd)
         | 
| 33 | 
            +
                    i.close
         | 
| 34 | 
            +
                    STDOUT.puts "#{Time.now} | * [#{path}] Uploading file to workspace '#{Local.workspace}'"
         | 
| 35 | 
            +
                    oe.each_line do |line|
         | 
| 36 | 
            +
                      next if line.chomp == '' || line.chomp == '---'
         | 
| 37 | 
            +
                      STDOUT.puts "#{Time.now} | #{line}"
         | 
| 38 | 
            +
                      STDOUT.puts "---"
         | 
| 39 39 | 
             
                    end
         | 
| 40 | 
            -
                     | 
| 40 | 
            +
                    thr.join
         | 
| 41 | 
            +
                    oe.close
         | 
| 42 | 
            +
                    status = thr.value
         | 
| 43 | 
            +
                    puts "* [#{path}] Error uploading static file '#{path}' to cloud workspace '#{Local.workspace}':" if status.exitstatus != 0
         | 
| 41 44 | 
             
                  end
         | 
| 42 45 |  | 
| 43 46 | 
             
                  def remove_from_workspace
         | 
    
        data/lib/faastruby/server/app.rb
    CHANGED
    
    | @@ -14,6 +14,7 @@ module FaaStRuby | |
| 14 14 | 
             
                set :root, SERVER_ROOT
         | 
| 15 15 | 
             
                set :public_folder, FaaStRuby::ProjectConfig.public_dir
         | 
| 16 16 | 
             
                set :static, true
         | 
| 17 | 
            +
                set :static_cache_control, [:must_revalidate, :proxy_revalidate, :max_age => 0]
         | 
| 17 18 | 
             
                register Sinatra::MultiRoute
         | 
| 18 19 | 
             
                before do
         | 
| 19 20 | 
             
                  cache_control :must_revalidate, :proxy_revalidate, :max_age => 0
         | 
    
        data/lib/faastruby/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: faastruby
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.5. | 
| 4 | 
            +
              version: 0.5.9
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Paulo Arruda
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: exe
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2019-03- | 
| 11 | 
            +
            date: 2019-03-12 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: rest-client
         | 
| @@ -339,7 +339,6 @@ files: | |
| 339 339 | 
             
            - lib/faastruby/server/response.rb
         | 
| 340 340 | 
             
            - lib/faastruby/server/runner.rb
         | 
| 341 341 | 
             
            - lib/faastruby/server/runner_methods.rb
         | 
| 342 | 
            -
            - lib/faastruby/server/sentinel.rb
         | 
| 343 342 | 
             
            - lib/faastruby/server/subscriber.rb
         | 
| 344 343 | 
             
            - lib/faastruby/spec_helper.rb
         | 
| 345 344 | 
             
            - lib/faastruby/supported_runtimes.rb
         | 
| @@ -1,496 +0,0 @@ | |
| 1 | 
            -
            # # Here 'function_folder' is the function folder.
         | 
| 2 | 
            -
            # require 'open3'
         | 
| 3 | 
            -
            # require 'tempfile'
         | 
| 4 | 
            -
            # require 'pathname'
         | 
| 5 | 
            -
            # require 'yaml'
         | 
| 6 | 
            -
            # require 'listen'
         | 
| 7 | 
            -
            # require 'colorize'
         | 
| 8 | 
            -
            # require 'securerandom'
         | 
| 9 | 
            -
             | 
| 10 | 
            -
            # module FaaStRuby
         | 
| 11 | 
            -
            #   module Sentinel
         | 
| 12 | 
            -
            #     extend FaaStRuby::Logger::System
         | 
| 13 | 
            -
            #     STATIC_FILES_SYNC_ENABLED = SYNC_ENABLED && FaaStRuby::ProjectConfig.public_dir?
         | 
| 14 | 
            -
            #     STOPPER_QUEUE = Queue.new
         | 
| 15 | 
            -
            #     @@threads = {}
         | 
| 16 | 
            -
            #     MUTEX = Mutex.new
         | 
| 17 | 
            -
            #     def self.quit
         | 
| 18 | 
            -
            #       Process.kill("HUP", FaaStRuby.sentinel_pid)
         | 
| 19 | 
            -
            #     end
         | 
| 20 | 
            -
             | 
| 21 | 
            -
            #     def self.add_thread(function_folder, key, value)
         | 
| 22 | 
            -
            #       MUTEX.synchronize do
         | 
| 23 | 
            -
            #         @@threads[function_folder] ||= {}
         | 
| 24 | 
            -
            #         @@threads[function_folder][key] = value
         | 
| 25 | 
            -
            #       end
         | 
| 26 | 
            -
            #     end
         | 
| 27 | 
            -
            #     def self.get_thread(function_folder, key)
         | 
| 28 | 
            -
            #       MUTEX.synchronize do
         | 
| 29 | 
            -
            #         return nil if @@threads[function_folder].nil?
         | 
| 30 | 
            -
            #         @@threads[function_folder][key]
         | 
| 31 | 
            -
            #       end
         | 
| 32 | 
            -
            #     end
         | 
| 33 | 
            -
             | 
| 34 | 
            -
            #     def self.get_threads
         | 
| 35 | 
            -
            #       MUTEX.synchronize do
         | 
| 36 | 
            -
            #         @@threads
         | 
| 37 | 
            -
            #       end
         | 
| 38 | 
            -
            #     end
         | 
| 39 | 
            -
             | 
| 40 | 
            -
            #     def self.tag
         | 
| 41 | 
            -
            #       '(Sentinel)'
         | 
| 42 | 
            -
            #     end
         | 
| 43 | 
            -
             | 
| 44 | 
            -
            #     def self.try_workspace
         | 
| 45 | 
            -
            #       puts "#{tag} Connecting to workspace '#{WORKSPACE_NAME}'..."
         | 
| 46 | 
            -
            #       try_to_create = Proc.new {system("faastruby create-workspace #{WORKSPACE_NAME}")}
         | 
| 47 | 
            -
            #       has_credentials = system("faastruby list-workspace #{WORKSPACE_NAME} > /dev/null 2>&1")
         | 
| 48 | 
            -
            #       continue = has_credentials || try_to_create.call
         | 
| 49 | 
            -
            #       unless continue
         | 
| 50 | 
            -
            #         puts "[FATAL] Unable to setup project workspace '#{WORKSPACE_NAME}'. Make sure you have the credentials, or try a different environment name.\nExample: faastruby local --sync --deploy-env #{DEPLOY_ENVIRONMENT}-#{(rand * 100).to_i}".red
         | 
| 51 | 
            -
            #         exit 1
         | 
| 52 | 
            -
            #       end
         | 
| 53 | 
            -
            #       true
         | 
| 54 | 
            -
            #     end
         | 
| 55 | 
            -
             | 
| 56 | 
            -
            #     def self.watch_for_live_compile(functions)
         | 
| 57 | 
            -
            #       functions.each do |path|
         | 
| 58 | 
            -
            #         # puts "Starting live compile for #{path}"
         | 
| 59 | 
            -
            #         watch_function_for_live_compile(path)
         | 
| 60 | 
            -
            #       end
         | 
| 61 | 
            -
            #     end
         | 
| 62 | 
            -
             | 
| 63 | 
            -
            #     def self.watch_function_for_live_compile(path)
         | 
| 64 | 
            -
            #       function_folder = File.expand_path path
         | 
| 65 | 
            -
            #       # FileUtils.rm_f("#{function_folder}/handler")
         | 
| 66 | 
            -
            #       # FileUtils.rm_f("#{function_folder}/handler.dwarf")
         | 
| 67 | 
            -
            #       add_thread(function_folder, 'watcher', start_watcher_for(function_folder))
         | 
| 68 | 
            -
            #       # This will force compile when the server starts
         | 
| 69 | 
            -
            #       trigger("#{function_folder}/faastruby.yml")
         | 
| 70 | 
            -
            #     end
         | 
| 71 | 
            -
             | 
| 72 | 
            -
            #     def self.start_watcher_for(function_folder)
         | 
| 73 | 
            -
            #       function_name = get_function_name(function_folder)
         | 
| 74 | 
            -
            #       puts "#{tag} Live compiling enabled for '#{function_name}'."
         | 
| 75 | 
            -
            #       exclude_from_watcher = [
         | 
| 76 | 
            -
            #         "#{function_folder}/handler",
         | 
| 77 | 
            -
            #         "#{function_folder}/handler.dwarf",
         | 
| 78 | 
            -
            #         "#{function_folder}/.package.zip"
         | 
| 79 | 
            -
            #       ]
         | 
| 80 | 
            -
            #       new_watcher_thread(function_folder, exclude_from_watcher, function_name)
         | 
| 81 | 
            -
            #     end
         | 
| 82 | 
            -
             | 
| 83 | 
            -
            #     def self.stop_watcher(listener, function_name)
         | 
| 84 | 
            -
            #       STOPPER_QUEUE << [listener, function_name]
         | 
| 85 | 
            -
            #     end
         | 
| 86 | 
            -
             | 
| 87 | 
            -
            #     def self.new_watcher_thread(function_folder, exclude_from_watcher, function_name)
         | 
| 88 | 
            -
            #       handler_path = get_handler_path_in(function_folder)
         | 
| 89 | 
            -
            #       listener = Listen.to(function_folder) do |modified, added, removed|
         | 
| 90 | 
            -
            #         full_path, relative_path, event = translate(modified, added, removed)
         | 
| 91 | 
            -
            #         next if exclude_from_watcher.include?(full_path)
         | 
| 92 | 
            -
            #         file_name = File.basename(full_path)
         | 
| 93 | 
            -
            #         puts "#{tag} Previous Job for '#{function_name}' aborted" if kill_thread_if_alive(function_folder, 'running', function_name)
         | 
| 94 | 
            -
            #         # puts "#{tag} Event: #{full_path}, #{event}"
         | 
| 95 | 
            -
            #         if event == :removed && is_a_function?(full_path, function_folder)
         | 
| 96 | 
            -
            #           puts "#{tag} Disabling watcher for function '#{function_name}'."
         | 
| 97 | 
            -
            #           stop_watcher(get_thread(function_folder, 'watcher'), function_name)
         | 
| 98 | 
            -
            #           # listener = get_thread(function_folder, 'watcher')
         | 
| 99 | 
            -
            #           # listener.stop
         | 
| 100 | 
            -
            #           # # Thread.current.exit
         | 
| 101 | 
            -
            #           next
         | 
| 102 | 
            -
            #         end
         | 
| 103 | 
            -
            #         add_thread(function_folder, 'running', Thread.new {CrystalBuild.new(function_folder, handler_path, run_before_build: true).start})
         | 
| 104 | 
            -
            #       end
         | 
| 105 | 
            -
            #       listener.start
         | 
| 106 | 
            -
            #       listener
         | 
| 107 | 
            -
            #     end
         | 
| 108 | 
            -
             | 
| 109 | 
            -
            #     def self.detect_new_functions(target, language)
         | 
| 110 | 
            -
            #       puts "#{tag} Watching for new #{language} functions..."
         | 
| 111 | 
            -
            #       only = /\/handler.rb$/ if language == 'Ruby'
         | 
| 112 | 
            -
            #       only = /\/handler.cr$/ if language == 'Crystal'
         | 
| 113 | 
            -
            #       listener = Listen.to(target, only: only) do |modified, added, removed|
         | 
| 114 | 
            -
            #         full_path, relative_path, event = translate(modified, added, removed)
         | 
| 115 | 
            -
            #         # Filewatcher.new(target).watch do |full_path, event|
         | 
| 116 | 
            -
            #         next unless event == :added
         | 
| 117 | 
            -
            #         file_name = File.basename(full_path)
         | 
| 118 | 
            -
            #         function_folder = File.dirname(full_path)
         | 
| 119 | 
            -
            #         function_name = get_function_name(function_folder)
         | 
| 120 | 
            -
            #         yield(function_folder, file_name, full_path, function_name)
         | 
| 121 | 
            -
            #         enable_sync_for(function_folder, delay: 1) if SYNC_ENABLED
         | 
| 122 | 
            -
            #       end
         | 
| 123 | 
            -
            #       listener.start
         | 
| 124 | 
            -
            #       listener
         | 
| 125 | 
            -
            #     end
         | 
| 126 | 
            -
             | 
| 127 | 
            -
            #     def self.start_crystal(functions)
         | 
| 128 | 
            -
            #       puts "#{tag} Crystal functions: #{functions}"
         | 
| 129 | 
            -
            #       enable_sync(functions, delay: 1) if SYNC_ENABLED
         | 
| 130 | 
            -
            #       watch_for_live_compile(functions) unless SYNC_ENABLED
         | 
| 131 | 
            -
            #       detect_new_functions(Dir.pwd, 'Crystal') do |function_folder, file, full_path, function_name|
         | 
| 132 | 
            -
            #         function_folder = normalize_crystal_folder(function_folder)
         | 
| 133 | 
            -
            #         add_configuration(function_folder, file, full_path)
         | 
| 134 | 
            -
            #         puts "#{tag} New Crystal function detected at '#{function_name}'."
         | 
| 135 | 
            -
            #         write_handler(full_path, 'crystal') unless File.size(full_path) > 0
         | 
| 136 | 
            -
            #         add_thread(function_folder, 'watcher', start_watcher_for(function_folder))
         | 
| 137 | 
            -
            #         # trigger(full_path)
         | 
| 138 | 
            -
            #       end
         | 
| 139 | 
            -
            #     end
         | 
| 140 | 
            -
             | 
| 141 | 
            -
            #     def self.start_ruby(functions)
         | 
| 142 | 
            -
            #       puts "#{tag} Ruby functions: #{functions}"
         | 
| 143 | 
            -
            #       enable_sync(functions) if SYNC_ENABLED
         | 
| 144 | 
            -
            #       listener = detect_new_functions(Dir.pwd, 'Ruby') do |function_folder, file, full_path, function_name|
         | 
| 145 | 
            -
            #         add_configuration(function_folder, file, full_path)
         | 
| 146 | 
            -
            #         puts "#{tag} New Ruby function detected at '#{function_name}'."
         | 
| 147 | 
            -
            #         write_handler(full_path, 'ruby') unless File.size(full_path) > 0
         | 
| 148 | 
            -
            #       end
         | 
| 149 | 
            -
            #     end
         | 
| 150 | 
            -
             | 
| 151 | 
            -
            #     def self.start_public
         | 
| 152 | 
            -
            #       puts "#{tag} Watching public folder '#{File.basename(FaaStRuby::ProjectConfig.public_dir)}'..."
         | 
| 153 | 
            -
            #       add_thread(FaaStRuby::ProjectConfig.public_dir, 'sync', start_public_sync)
         | 
| 154 | 
            -
            #     end
         | 
| 155 | 
            -
             | 
| 156 | 
            -
            #     ########################
         | 
| 157 | 
            -
             | 
| 158 | 
            -
            #     def self.start_public_sync(delay: nil)
         | 
| 159 | 
            -
            #       sleep delay if delay
         | 
| 160 | 
            -
            #       public_dir = FaaStRuby::ProjectConfig.public_dir
         | 
| 161 | 
            -
            #       listener = Listen.to(public_dir) do |modified, added, removed|
         | 
| 162 | 
            -
            #         full_path, relative_path, event = translate(modified, added, removed)
         | 
| 163 | 
            -
            #         # puts "FULL_PATH: #{full_path}"
         | 
| 164 | 
            -
            #         # puts "RELATIVE_PATH: #{relative_path}"
         | 
| 165 | 
            -
            #         # puts "EVENT: #{event}"
         | 
| 166 | 
            -
            #         # next if File.directory?(full_path)
         | 
| 167 | 
            -
            #         file_name = File.basename(full_path)
         | 
| 168 | 
            -
            #         puts "#{tag} Previous upload of '#{FaaStRuby::ProjectConfig.public_dir(absolute: false)}/#{relative_path}' aborted" if kill_thread_if_alive(full_path, 'deploying', full_path)
         | 
| 169 | 
            -
             | 
| 170 | 
            -
            #         if event == :removed
         | 
| 171 | 
            -
            #           cmd = "faastruby rm #{WORKSPACE_NAME}:/#{relative_path}"
         | 
| 172 | 
            -
            #           puts "#{tag} Running: #{cmd}"
         | 
| 173 | 
            -
            #           system(cmd)
         | 
| 174 | 
            -
            #           add_thread(full_path, 'deployed', nil)
         | 
| 175 | 
            -
            #           next
         | 
| 176 | 
            -
            #         end
         | 
| 177 | 
            -
            #         # deploy_cmd, deploy_cmd_print = faastruby_cp(filename)
         | 
| 178 | 
            -
            #         cmd = "faastruby cp #{full_path} #{WORKSPACE_NAME}:/#{relative_path}"
         | 
| 179 | 
            -
            #         puts "#{tag} Running: #{cmd}"
         | 
| 180 | 
            -
            #         add_thread(full_path, 'deploying', Thread.new {system("cd #{SERVER_ROOT} && #{cmd}")})
         | 
| 181 | 
            -
            #         add_thread(full_path, 'deployed', true)
         | 
| 182 | 
            -
            #       end
         | 
| 183 | 
            -
            #       listener.start
         | 
| 184 | 
            -
            #       listener
         | 
| 185 | 
            -
            #     end
         | 
| 186 | 
            -
             | 
| 187 | 
            -
            #     def self.translate(modified, added, removed)
         | 
| 188 | 
            -
            #       return [modified[0], relative_path_for(modified[0].dup), :modified] if modified.any?
         | 
| 189 | 
            -
            #       return [added[0], relative_path_for(added[0].dup), :added] if added.any?
         | 
| 190 | 
            -
            #       return [removed[0], relative_path_for(removed[0].dup), :removed] if removed.any?
         | 
| 191 | 
            -
            #     end
         | 
| 192 | 
            -
             | 
| 193 | 
            -
            #     # def self.final_path_and_subject(full_path)
         | 
| 194 | 
            -
            #     #   relative_path = relative_path_for(full_path)
         | 
| 195 | 
            -
            #     #   prefix = relative_path.slice!(/^(public|functions)\//)
         | 
| 196 | 
            -
            #     #   subject = :static if File.expand_path(prefix) == "#{FaaStRuby::ProjectConfig.public_dir}"
         | 
| 197 | 
            -
            #     #   subject = :function if File.expand_path(prefix) == "#{FaaStRuby::ProjectConfig.functions_dir}"
         | 
| 198 | 
            -
            #     #   [relative_path_for(full_path), subject]
         | 
| 199 | 
            -
            #     # end
         | 
| 200 | 
            -
             | 
| 201 | 
            -
            #     def self.relative_path_for(full_path)
         | 
| 202 | 
            -
            #       full_path.slice!("#{SERVER_ROOT}/#{FaaStRuby::ProjectConfig.public_dir(absolute: false)}/")
         | 
| 203 | 
            -
            #       full_path
         | 
| 204 | 
            -
            #     end
         | 
| 205 | 
            -
             | 
| 206 | 
            -
            #     ####################
         | 
| 207 | 
            -
             | 
| 208 | 
            -
            #     def self.enable_sync(functions, delay: nil)
         | 
| 209 | 
            -
            #       functions.each do |path|
         | 
| 210 | 
            -
            #         function_folder = File.expand_path path
         | 
| 211 | 
            -
            #         enable_sync_for(function_folder, delay: delay)
         | 
| 212 | 
            -
            #       end
         | 
| 213 | 
            -
            #     end
         | 
| 214 | 
            -
             | 
| 215 | 
            -
            #     def self.enable_sync_for(function_folder, delay: nil)
         | 
| 216 | 
            -
            #       add_thread(function_folder, 'sync', start_sync_for(function_folder, delay: delay))
         | 
| 217 | 
            -
            #     end
         | 
| 218 | 
            -
             | 
| 219 | 
            -
            #     def self.start_listener_monitor
         | 
| 220 | 
            -
            #       Thread.new do
         | 
| 221 | 
            -
            #         loop do
         | 
| 222 | 
            -
            #           listener, function_name = STOPPER_QUEUE.pop
         | 
| 223 | 
            -
            #           listener.stop
         | 
| 224 | 
            -
            #           puts "#{tag} Watcher for function '#{function_name}' was stopped."
         | 
| 225 | 
            -
            #         end
         | 
| 226 | 
            -
            #       end
         | 
| 227 | 
            -
            #     end
         | 
| 228 | 
            -
             | 
| 229 | 
            -
            #     def self.start!
         | 
| 230 | 
            -
            #       Dir.chdir FaaStRuby::ProjectConfig.functions_dir
         | 
| 231 | 
            -
            #       start_listener_monitor
         | 
| 232 | 
            -
            #       functions = find_functions
         | 
| 233 | 
            -
            #       listeners = {}
         | 
| 234 | 
            -
            #       puts "#{tag} Sync mode enabled. Your functions will be auto-deployed to the workspace '#{WORKSPACE_NAME}'." if SYNC_ENABLED
         | 
| 235 | 
            -
            #       listeners['ruby'] = start_ruby(functions['ruby']) if RUBY_ENABLED
         | 
| 236 | 
            -
            #       listeners['crystal'] =  start_crystal(functions['crystal']) if CRYSTAL_ENABLED
         | 
| 237 | 
            -
            #       # aaa # gotta finish configure public sync
         | 
| 238 | 
            -
            #       listeners['public'] = start_public if STATIC_FILES_SYNC_ENABLED
         | 
| 239 | 
            -
            #       function_listeners = listeners['crystal'].merge(listeners['ruby'])
         | 
| 240 | 
            -
            #       function_listeners.each do
         | 
| 241 | 
            -
            #       sleep
         | 
| 242 | 
            -
            #     ensure
         | 
| 243 | 
            -
            #       listeners.each {|language, listener| listener.stop}
         | 
| 244 | 
            -
            #     end
         | 
| 245 | 
            -
             | 
| 246 | 
            -
            #     def self.normalize_crystal_folder(function_folder)
         | 
| 247 | 
            -
            #       if function_folder.match(/src$/) && File.file?("#{function_folder}/../faastruby.yml") && File.file?("#{function_folder}/handler.cr")
         | 
| 248 | 
            -
            #         function_folder.sub!(/\/src$/, '')
         | 
| 249 | 
            -
            #       end
         | 
| 250 | 
            -
            #       function_folder
         | 
| 251 | 
            -
            #     end
         | 
| 252 | 
            -
             | 
| 253 | 
            -
            #     def self.write_handler(filename, runtime)
         | 
| 254 | 
            -
            #       content = "def handler(event)\n  # Write code here\n  \nend" if runtime == 'ruby'
         | 
| 255 | 
            -
            #       content = "def handler(event : FaaStRuby::Event) : FaaStRuby::Response\n  # Write code here\n  \nend" if runtime == 'crystal'
         | 
| 256 | 
            -
            #       File.write(filename, content)
         | 
| 257 | 
            -
            #     end
         | 
| 258 | 
            -
             | 
| 259 | 
            -
            #     def self.get_function_name(function_folder)
         | 
| 260 | 
            -
            #       # f_dir = FaaStRuby::ProjectConfig.functions_dir.dup
         | 
| 261 | 
            -
            #       # f_dir.slice!(function_folder)
         | 
| 262 | 
            -
            #       # f_dir
         | 
| 263 | 
            -
            #       (function_folder.split('/') - FaaStRuby::ProjectConfig.functions_dir.split('/')).join('/')
         | 
| 264 | 
            -
            #     end
         | 
| 265 | 
            -
             | 
| 266 | 
            -
            #     def self.add_configuration(function_folder, file, filename)
         | 
| 267 | 
            -
            #       if File.file?("#{function_folder}/faastruby.yml")
         | 
| 268 | 
            -
            #         merge_yaml(function_folder, runtime: default_runtime(file))
         | 
| 269 | 
            -
            #       else
         | 
| 270 | 
            -
            #         write_yaml(function_folder, runtime: default_runtime(file))
         | 
| 271 | 
            -
            #       end
         | 
| 272 | 
            -
            #     end
         | 
| 273 | 
            -
             | 
| 274 | 
            -
            #     def self.default_runtime(handler)
         | 
| 275 | 
            -
            #       case handler
         | 
| 276 | 
            -
            #       when 'handler.rb'
         | 
| 277 | 
            -
            #         return DEFAULT_RUBY_RUNTIME
         | 
| 278 | 
            -
            #       when 'handler.cr'
         | 
| 279 | 
            -
            #         return DEFAULT_CRYSTAL_RUNTIME
         | 
| 280 | 
            -
            #       end
         | 
| 281 | 
            -
            #     end
         | 
| 282 | 
            -
             | 
| 283 | 
            -
            #     def self.trigger(file)
         | 
| 284 | 
            -
            #       Thread.new do
         | 
| 285 | 
            -
            #         sleep 0.5
         | 
| 286 | 
            -
            #         FileUtils.touch(file)
         | 
| 287 | 
            -
            #         Thread.exit
         | 
| 288 | 
            -
            #       end
         | 
| 289 | 
            -
            #     end
         | 
| 290 | 
            -
             | 
| 291 | 
            -
            #     def self.merge_yaml(function_folder, runtime:)
         | 
| 292 | 
            -
            #       yaml = YAML.load(File.read("#{function_folder}/faastruby.yml"))
         | 
| 293 | 
            -
            #       write_yaml(function_folder, runtime: runtime, original: yaml)
         | 
| 294 | 
            -
            #     end
         | 
| 295 | 
            -
             | 
| 296 | 
            -
            #     def self.write_yaml(function_folder, runtime:, original: nil)
         | 
| 297 | 
            -
            #       function_name = get_function_name(function_folder)
         | 
| 298 | 
            -
            #       hash = {
         | 
| 299 | 
            -
            #         'cli_version' => FaaStRuby::VERSION,
         | 
| 300 | 
            -
            #         'name' => function_name,
         | 
| 301 | 
            -
            #         'runtime' => runtime
         | 
| 302 | 
            -
            #       }
         | 
| 303 | 
            -
            #       hash = original.merge(hash) if original
         | 
| 304 | 
            -
            #       File.write("#{function_folder}/faastruby.yml", hash.to_yaml)
         | 
| 305 | 
            -
            #       # unless original
         | 
| 306 | 
            -
            #         File.open("#{function_folder}/faastruby.yml", 'a') do |f|
         | 
| 307 | 
            -
            #           f.write yaml_comments
         | 
| 308 | 
            -
            #         end
         | 
| 309 | 
            -
            #       # end
         | 
| 310 | 
            -
            #       puts "#{tag} File created: '#{function_name}/faastruby.yml'"
         | 
| 311 | 
            -
            #     end
         | 
| 312 | 
            -
             | 
| 313 | 
            -
            #     def self.yaml_comments
         | 
| 314 | 
            -
            #       [
         | 
| 315 | 
            -
            #         '## You can add commands to run locally before building the deployment package.',
         | 
| 316 | 
            -
            #         "## Some use cases are:",
         | 
| 317 | 
            -
            #         "## * minifying Javascript/CSS",
         | 
| 318 | 
            -
            #         "## * downloading a file to be included in the package.",
         | 
| 319 | 
            -
            #         "# before_build:",
         | 
| 320 | 
            -
            #         "#   - curl https://some.url --output some.file",
         | 
| 321 | 
            -
            #         "#   - uglifyjs your.js -c -m -o your.min.js",
         | 
| 322 | 
            -
            #         '',
         | 
| 323 | 
            -
            #         '## To schedule periodic runs, follow the example below:',
         | 
| 324 | 
            -
            #         '# schedule:',
         | 
| 325 | 
            -
            #         '#   job1:',
         | 
| 326 | 
            -
            #         '#     when: every 2 hours',
         | 
| 327 | 
            -
            #         '#     body: {"foo": "bar"}',
         | 
| 328 | 
            -
            #         '#     method: POST',
         | 
| 329 | 
            -
            #         '#     query_params: {"param": "value"}',
         | 
| 330 | 
            -
            #         '#     headers: {"Content-Type": "application/json"}',
         | 
| 331 | 
            -
            #         '#   job2: ...'
         | 
| 332 | 
            -
            #       ].join("\n")
         | 
| 333 | 
            -
            #     end
         | 
| 334 | 
            -
             | 
| 335 | 
            -
            #     def self.start_sync_for(function_folder, delay: nil)
         | 
| 336 | 
            -
            #       # function_name = get_function_name(function_folder)
         | 
| 337 | 
            -
            #       # puts "#{tag} Sync activated for function '#{function_name}'."
         | 
| 338 | 
            -
            #       Thread.new do
         | 
| 339 | 
            -
            #         start_sync(function_folder, delay: delay)
         | 
| 340 | 
            -
            #       end
         | 
| 341 | 
            -
            #     end
         | 
| 342 | 
            -
             | 
| 343 | 
            -
            #     def self.remove_from_cloud(function_name, function_folder)
         | 
| 344 | 
            -
            #       remove_cmd = ["faastruby", "remove-from", WORKSPACE_NAME, "-y", "-f", function_name]
         | 
| 345 | 
            -
            #       removed = system(*remove_cmd)
         | 
| 346 | 
            -
            #       add_thread(function_folder, 'deployed', nil)
         | 
| 347 | 
            -
            #       if removed
         | 
| 348 | 
            -
            #         puts "#{tag} Function '#{function_name}' was removed from the cloud."
         | 
| 349 | 
            -
            #       else
         | 
| 350 | 
            -
            #         puts "#{tag} The workspace '#{WORKSPACE_NAME}' had no function named '#{function_name}', please ignore the message in red."
         | 
| 351 | 
            -
            #       end
         | 
| 352 | 
            -
            #     end
         | 
| 353 | 
            -
             | 
| 354 | 
            -
            #     def self.kill_thread_if_alive(function_folder, kind, function_name)
         | 
| 355 | 
            -
            #       thr = get_thread(function_folder, kind)
         | 
| 356 | 
            -
            #       if thr&.alive?
         | 
| 357 | 
            -
            #         Thread.kill(thr)
         | 
| 358 | 
            -
            #         return true
         | 
| 359 | 
            -
            #       end
         | 
| 360 | 
            -
            #       return false
         | 
| 361 | 
            -
            #     end
         | 
| 362 | 
            -
             | 
| 363 | 
            -
            #     def self.start_sync(function_folder, delay: nil)
         | 
| 364 | 
            -
            #       sleep delay if delay
         | 
| 365 | 
            -
            #       function_name = get_function_name(function_folder)
         | 
| 366 | 
            -
            #       exclude_from_watcher = [
         | 
| 367 | 
            -
            #         "#{function_folder}/handler",
         | 
| 368 | 
            -
            #         "#{function_folder}/handler.dwarf",
         | 
| 369 | 
            -
            #         "#{function_folder}/.package.zip"
         | 
| 370 | 
            -
            #       ]
         | 
| 371 | 
            -
            #       listener = Listen.to(function_folder) do |modified, added, removed|
         | 
| 372 | 
            -
            #         full_path, relative_path, event = translate(modified, added, removed)
         | 
| 373 | 
            -
            #         next if exclude_from_watcher.include?(full_path)
         | 
| 374 | 
            -
            #         puts "#{tag} Previous Deploy for '#{function_name}' aborted" if kill_thread_if_alive(function_folder, 'deploying', function_name)
         | 
| 375 | 
            -
            #         if event == :removed && is_a_function?(full_path, function_folder)
         | 
| 376 | 
            -
            #           remove_from_project(function_folder, function_name)
         | 
| 377 | 
            -
            #           next
         | 
| 378 | 
            -
            #         end
         | 
| 379 | 
            -
            #         deploy_cmd, deploy_cmd_print = generate_deploy_command(function_name, function_folder)
         | 
| 380 | 
            -
            #         puts "#{tag} Running: #{deploy_cmd_print.join(' ')}"
         | 
| 381 | 
            -
            #         deploy(function_folder, deploy_cmd)
         | 
| 382 | 
            -
            #       end
         | 
| 383 | 
            -
            #       listener.start
         | 
| 384 | 
            -
            #       listener
         | 
| 385 | 
            -
            #     end
         | 
| 386 | 
            -
             | 
| 387 | 
            -
            #     def self.is_a_function?(filename, function_folder)
         | 
| 388 | 
            -
            #       filename == function_folder || filename.match(/#{function_folder}\/(handler\.(rb|cr)|faastruby.yml)/)
         | 
| 389 | 
            -
            #     end
         | 
| 390 | 
            -
             | 
| 391 | 
            -
            #     def self.remove_from_project(function_folder, function_name)
         | 
| 392 | 
            -
            #       Thread.kill(get_thread(function_folder, 'sync'))
         | 
| 393 | 
            -
            #       add_thread(function_folder, 'sync', nil)
         | 
| 394 | 
            -
            #       puts "#{tag} Function '#{function_name}' was removed from the project."
         | 
| 395 | 
            -
            #       remove_from_cloud(function_name, function_folder)# if get_thread(function_folder, 'deployed')
         | 
| 396 | 
            -
            #     end
         | 
| 397 | 
            -
             | 
| 398 | 
            -
            #     def self.deploy(function_folder, deploy_cmd)
         | 
| 399 | 
            -
            #       add_thread(function_folder, 'deploying', Thread.new {system(*deploy_cmd)})
         | 
| 400 | 
            -
            #       add_thread(function_folder, 'deployed', true)
         | 
| 401 | 
            -
            #     end
         | 
| 402 | 
            -
             | 
| 403 | 
            -
            #     def self.generate_deploy_command(function_name, function_folder)
         | 
| 404 | 
            -
            #       project_config = FaaStRuby::ProjectConfig.project_config
         | 
| 405 | 
            -
            #       deploy_cmd = ['faastruby', 'deploy-to', WORKSPACE_NAME, '-f', function_name]
         | 
| 406 | 
            -
            #       deploy_cmd << '--set-root' if FaaStRuby::ProjectConfig.root_to == function_name
         | 
| 407 | 
            -
            #       deploy_cmd << '--set-catch-all' if FaaStRuby::ProjectConfig.catch_all == function_name
         | 
| 408 | 
            -
            #       secrets_json = Oj.dump(FaaStRuby::ProjectConfig.secrets_for_function(function_name)) rescue nil
         | 
| 409 | 
            -
            #       deploy_cmd_print = deploy_cmd
         | 
| 410 | 
            -
            #       if secrets_json
         | 
| 411 | 
            -
            #         deploy_cmd += ["--context", secrets_json]
         | 
| 412 | 
            -
            #         deploy_cmd_print += ["--context", '*REDACTED*']
         | 
| 413 | 
            -
            #       end
         | 
| 414 | 
            -
            #       [deploy_cmd, deploy_cmd_print]
         | 
| 415 | 
            -
            #     end
         | 
| 416 | 
            -
             | 
| 417 | 
            -
            #     def self.get_handler_path_in(function_folder)
         | 
| 418 | 
            -
            #       if File.file?("#{function_folder}/handler.cr")
         | 
| 419 | 
            -
            #         "#{function_folder}/handler"
         | 
| 420 | 
            -
            #       else
         | 
| 421 | 
            -
            #         "#{function_folder}/src/handler"
         | 
| 422 | 
            -
            #       end
         | 
| 423 | 
            -
            #     end
         | 
| 424 | 
            -
             | 
| 425 | 
            -
            #     def self.check_for_yaml_file(function_folder, handler_file)
         | 
| 426 | 
            -
            #       yaml_file = "#{function_folder}/faastruby.yml"
         | 
| 427 | 
            -
            #       unless File.file?(yaml_file)
         | 
| 428 | 
            -
            #         puts "#{tag} Function '#{function_folder}' did not have a YML configuration file."
         | 
| 429 | 
            -
            #         write_yaml(function_folder, runtime: default_runtime(File.basename(handler_file)), original: nil)
         | 
| 430 | 
            -
            #       end
         | 
| 431 | 
            -
            #       YAML.load(File.read yaml_file)
         | 
| 432 | 
            -
            #     end
         | 
| 433 | 
            -
             | 
| 434 | 
            -
            #     def self.find_functions
         | 
| 435 | 
            -
            #       crystal_functions = []
         | 
| 436 | 
            -
            #       ruby_functions = []
         | 
| 437 | 
            -
            #       Dir.glob(["**/handler.rb", "**/handler.cr"]).each do |handler_file|
         | 
| 438 | 
            -
            #         function_folder = File.dirname(handler_file)
         | 
| 439 | 
            -
            #         function_folder.sub!(/\/src$/, '') if handler_file.match(/src\/handler\.cr$/)
         | 
| 440 | 
            -
            #         yaml = check_for_yaml_file(function_folder, handler_file)
         | 
| 441 | 
            -
            #         case yaml['runtime']
         | 
| 442 | 
            -
            #         when /^crystal:/
         | 
| 443 | 
            -
            #           crystal_functions << function_folder
         | 
| 444 | 
            -
            #         when /^ruby:/
         | 
| 445 | 
            -
            #           ruby_functions << function_folder
         | 
| 446 | 
            -
            #         end
         | 
| 447 | 
            -
            #       end
         | 
| 448 | 
            -
            #       {'crystal' => crystal_functions, 'ruby' => ruby_functions}
         | 
| 449 | 
            -
            #     end
         | 
| 450 | 
            -
            #   end
         | 
| 451 | 
            -
             | 
| 452 | 
            -
            #   class CrystalBuild
         | 
| 453 | 
            -
            #     include FaaStRuby::Logger::System
         | 
| 454 | 
            -
            #     def initialize(directory, handler_path, run_before_build: false)
         | 
| 455 | 
            -
            #       @directory = directory
         | 
| 456 | 
            -
            #       @function_name = Sentinel.get_function_name(directory)
         | 
| 457 | 
            -
            #       @runtime_path = Pathname.new "#{Gem::Specification.find_by_name("faastruby").gem_dir}/lib/faastruby/server/crystal_runtime.cr"
         | 
| 458 | 
            -
            #       h_path = Pathname.new(handler_path)
         | 
| 459 | 
            -
            #       @handler_path = h_path.relative_path_from @runtime_path
         | 
| 460 | 
            -
            #       @env = {'HANDLER_PATH' => @handler_path.to_s}
         | 
| 461 | 
            -
            #       @run_before_build = run_before_build
         | 
| 462 | 
            -
            #       @crystal_build = "cd #{@directory} && crystal build #{@runtime_path} -o handler"
         | 
| 463 | 
            -
            #     end
         | 
| 464 | 
            -
             | 
| 465 | 
            -
            #     def pre_compile_list
         | 
| 466 | 
            -
            #       return [] unless @run_before_build
         | 
| 467 | 
            -
            #       YAML.load(File.read("#{@directory}/faastruby.yml"))["before_build"] || []
         | 
| 468 | 
            -
            #     end
         | 
| 469 | 
            -
             | 
| 470 | 
            -
            #     def precompile
         | 
| 471 | 
            -
            #       pre_compile_list.each do |cmd|
         | 
| 472 | 
            -
            #         cmd = "cd #{@directory} && #{cmd}"
         | 
| 473 | 
            -
            #         puts "#{tag} Job ID=\"#{job_id}\" running before_build: '#{cmd}'"
         | 
| 474 | 
            -
            #         output, status = Open3.capture2e(cmd)
         | 
| 475 | 
            -
            #         success = status.exitstatus == 0
         | 
| 476 | 
            -
            #         unless success
         | 
| 477 | 
            -
            #           puts "#{tag} Job ID=\"#{job_id}\": #{output}"
         | 
| 478 | 
            -
            #           puts "#{tag} Job ID=\"#{job_id}\" failed: #{status}"
         | 
| 479 | 
            -
            #           return false
         | 
| 480 | 
            -
            #         end
         | 
| 481 | 
            -
            #       end
         | 
| 482 | 
            -
            #       return true
         | 
| 483 | 
            -
            #     end
         | 
| 484 | 
            -
             | 
| 485 | 
            -
            #     def start
         | 
| 486 | 
            -
            #       Thread.report_on_exception = false
         | 
| 487 | 
            -
            #       job_id = SecureRandom.uuid
         | 
| 488 | 
            -
            #       puts "#{tag} Job ID=\"#{job_id}\" started: Compiling function '#{@function_name}'"
         | 
| 489 | 
            -
            #       return false unless precompile
         | 
| 490 | 
            -
            #       output, status = Open3.capture2e(@env, @crystal_build)
         | 
| 491 | 
            -
            #       success = status.exitstatus == 0
         | 
| 492 | 
            -
            #       puts "#{tag} Job ID=\"#{job_id}\": #{output}" unless success
         | 
| 493 | 
            -
            #       puts "#{tag} Job ID=\"#{job_id}\" #{success ? 'completed' : 'failed'}: #{status}"
         | 
| 494 | 
            -
            #     end
         | 
| 495 | 
            -
            #   end
         | 
| 496 | 
            -
            # end
         |