Class: FaaStRuby::Local::Function
- Inherits:
-
Object
- Object
- FaaStRuby::Local::Function
- Extended by:
- Logger
- Includes:
- Logger
- Defined in:
- lib/faastruby/local/functions/function.rb
Instance Attribute Summary collapse
-
#absolute_folder ⇒ Object
Instance methods.
-
#before_build ⇒ Object
Instance methods.
-
#name ⇒ Object
Instance methods.
Class Method Summary collapse
- .find_all_in(functions_dir) ⇒ Object
- .from_yaml(absolute_folder) ⇒ Object
- .get_function_folder_for(entry) ⇒ Object
- .that_has_file(entry, event_type) ⇒ Object
Instance Method Summary collapse
- #compile ⇒ Object
- #deploy ⇒ Object
- #generate_deploy_command ⇒ Object
-
#initialize(name:, before_build: [], absolute_folder:) ⇒ Function
constructor
A new instance of Function.
- #initialize_new_function ⇒ Object
- #language ⇒ Object
- #load_yaml ⇒ Object
- #merge_yaml(hash, yaml_file) ⇒ Object
- #remove_from_workspace ⇒ Object
- #write_yaml ⇒ Object
- #yaml_comments ⇒ Object
Methods included from Logger
Constructor Details
#initialize(name:, before_build: [], absolute_folder:) ⇒ Function
Returns a new instance of Function.
56 57 58 59 60 61 |
# File 'lib/faastruby/local/functions/function.rb', line 56 def initialize(name:, before_build: [], absolute_folder:) debug "initialize(name: #{name.inspect}, before_build: #{before_build.inspect}, absolute_folder: #{absolute_folder.inspect})" @name = name @before_build = before_build || [] @absolute_folder = absolute_folder end |
Instance Attribute Details
#absolute_folder ⇒ Object
Instance methods
55 56 57 |
# File 'lib/faastruby/local/functions/function.rb', line 55 def absolute_folder @absolute_folder end |
#before_build ⇒ Object
Instance methods
55 56 57 |
# File 'lib/faastruby/local/functions/function.rb', line 55 def before_build @before_build end |
#name ⇒ Object
Instance methods
55 56 57 |
# File 'lib/faastruby/local/functions/function.rb', line 55 def name @name end |
Class Method Details
.find_all_in(functions_dir) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/faastruby/local/functions/function.rb', line 7 def self.find_all_in(functions_dir) debug "self.find_all_in(#{functions_dir.inspect})" Dir.glob(["**/handler.rb", "**/handler.cr"], base: functions_dir).map do |entry| function_absolute_folder = "#{functions_dir}/#{File.dirname(entry)}" next unless File.file?("#{function_absolute_folder}/faastruby.yml") from_yaml(function_absolute_folder) end.compact end |
.from_yaml(absolute_folder) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/faastruby/local/functions/function.rb', line 31 def self.from_yaml(absolute_folder) debug "self.from_yaml(#{absolute_folder.inspect})" yaml_file = "#{absolute_folder}/faastruby.yml" yaml = YAML.load(File.read(yaml_file)) language, runtime_version = yaml['runtime'].split(':') object = Local::RubyFunction if language == 'ruby' object = Local::CrystalFunction if language == 'crystal' object.new( name: yaml['name'], before_build: yaml['before_build'], absolute_folder: absolute_folder ) end |
.get_function_folder_for(entry) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/faastruby/local/functions/function.rb', line 45 def self.get_function_folder_for(entry) return File.dirname(entry) if File.basename(entry) == 'faastruby.yml' debug "self.get_function_folder_for(#{entry.inspect})" dirname = File.dirname(entry) raise MissingConfigurationFileError.new("ERROR: Could not determine which function the file belongs to. Make sure your functions have the configuration file 'faastruby.yml'.") if dirname == SERVER_ROOT return dirname if File.file?("#{dirname}/faastruby.yml") get_function_folder_for(dirname) end |
.that_has_file(entry, event_type) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/faastruby/local/functions/function.rb', line 16 def self.that_has_file(entry, event_type) debug "self.that_has_file(#{entry.inspect})" absolute_folder = get_function_folder_for(entry) if event_type == :removed name = absolute_folder.dup name.slice!("#{Local.functions_dir}/") return new( name: name, before_build: [], absolute_folder: absolute_folder ) end from_yaml(absolute_folder) end |
Instance Method Details
#compile ⇒ Object
104 105 106 107 |
# File 'lib/faastruby/local/functions/function.rb', line 104 def compile debug "compile" true end |
#deploy ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/faastruby/local/functions/function.rb', line 63 def deploy debug "deploy" deploy_cmd, deploy_cmd_print = generate_deploy_command puts "Running: #{deploy_cmd_print.join(' ')}" output, status = Open3.capture2e(deploy_cmd.join(' ')) STDOUT.puts "#{Time.now} | " + "* [#{name}] Deploying...".green STDOUT.puts "---" String.disable_colorization = true if status.exitstatus == 0 output.split("\n").each {|o| puts o unless o == '---'} else puts "* [#{name}] Deploy Failed:" STDERR.puts output end String.disable_colorization = false end |
#generate_deploy_command ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/faastruby/local/functions/function.rb', line 89 def generate_deploy_command debug "generate_deploy_command" project_config = Local.project_config deploy_cmd = ['faastruby', 'deploy-to', Local.workspace, '-f', @absolute_folder, '--dont-create-workspace'] deploy_cmd << '--set-root' if Local.root_to == @name deploy_cmd << '--set-catch-all' if Local.catch_all == @name secrets_json = Oj.dump(Local.secrets_for_function(@name)) rescue nil deploy_cmd_print = deploy_cmd if secrets_json deploy_cmd += ["--context", secrets_json] deploy_cmd_print += ["--context", '*REDACTED*'] end [deploy_cmd, deploy_cmd_print] end |
#initialize_new_function ⇒ Object
120 121 122 123 124 |
# File 'lib/faastruby/local/functions/function.rb', line 120 def initialize_new_function debug "initialize_new_function" write_yaml write_handler end |
#language ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/faastruby/local/functions/function.rb', line 80 def language case YAML.load(File.read("#{@absolute_folder}/faastruby.yml"))['runtime'] when /^ruby:/ "ruby" when /^crystal:/ "crystal" end end |
#load_yaml ⇒ Object
132 133 134 135 |
# File 'lib/faastruby/local/functions/function.rb', line 132 def load_yaml debug "load_yaml" YAML.load(File.read("#{@absolute_folder}/faastruby.yml")) end |
#merge_yaml(hash, yaml_file) ⇒ Object
126 127 128 129 130 |
# File 'lib/faastruby/local/functions/function.rb', line 126 def merge_yaml(hash, yaml_file) debug "merge_yaml(#{hash.inspect}, #{yaml_file.inspect})" new_config = load_yaml.merge(hash) File.write(yaml_file, new_config.to_yaml) end |
#remove_from_workspace ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/faastruby/local/functions/function.rb', line 109 def remove_from_workspace debug "remove_from_workspace" remove_cmd = ["faastruby", "remove-from", Local.workspace, "-y", "-f", @name] puts "Removing function '#{@name}' from the cloud workspace '#{Local.workspace}'." removed = system(*remove_cmd) STDOUT.puts '---' if removed puts "Function '#{@name}' was removed from the cloud workspace '#{Local.workspace}'." end end |
#write_yaml ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/faastruby/local/functions/function.rb', line 137 def write_yaml debug "write_yaml" yaml_file = "#{@absolute_folder}/faastruby.yml" if File.file?(yaml_file) merge_yaml(yaml_hash, yaml_file) else File.write(yaml_file, yaml_hash.to_yaml) end File.open(yaml_file, 'a') do |f| f.write yaml_comments end end |
#yaml_comments ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/faastruby/local/functions/function.rb', line 150 def yaml_comments [ '## You can add commands to run locally before building the deployment package.', "## Some use cases are:", "## * minifying Javascript/CSS", "## * downloading a file to be included in the package.", "# before_build:", "# - curl https://some.url --output some.file", "# - uglifyjs your.js -c -m -o your.min.js", '', '## To schedule periodic runs, follow the example below:', '# schedule:', '# job1:', '# when: every 2 hours', '# body: {"foo": "bar"}', '# method: POST', '# query_params: {"param": "value"}', '# headers: {"Content-Type": "application/json"}', '# job2: ...' ].join("\n") end |