Class: FaaStRuby::Local::Function

Inherits:
Object
  • Object
show all
Extended by:
Logger
Includes:
Logger
Defined in:
lib/faastruby/local/functions/function.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

debug, puts, puts

Constructor Details

#initialize(name:, before_build: [], absolute_folder:) ⇒ Function

Returns a new instance of Function.



57
58
59
60
61
62
# File 'lib/faastruby/local/functions/function.rb', line 57

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_folderObject

Instance methods



56
57
58
# File 'lib/faastruby/local/functions/function.rb', line 56

def absolute_folder
  @absolute_folder
end

#before_buildObject

Instance methods



56
57
58
# File 'lib/faastruby/local/functions/function.rb', line 56

def before_build
  @before_build
end

#nameObject

Instance methods



56
57
58
# File 'lib/faastruby/local/functions/function.rb', line 56

def name
  @name
end

Class Method Details

.find_all_in(functions_dir) ⇒ Object



7
8
9
10
11
12
13
14
15
# 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)}"
    function_absolute_folder = File.dirname(function_absolute_folder) if File.basename(function_absolute_folder) == "src"
    next unless File.file?("#{function_absolute_folder}/faastruby.yml")
    from_yaml(function_absolute_folder)
  end.compact
end

.from_yaml(absolute_folder) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/faastruby/local/functions/function.rb', line 32

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



46
47
48
49
50
51
52
53
# File 'lib/faastruby/local/functions/function.rb', line 46

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/faastruby/local/functions/function.rb', line 17

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

#compileObject



103
104
105
106
# File 'lib/faastruby/local/functions/function.rb', line 103

def compile
  debug "compile"
  true
end

#deployObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/faastruby/local/functions/function.rb', line 64

def deploy
  debug "deploy"
  deploy_cmd, deploy_cmd_print = generate_deploy_command
  puts "Running: #{deploy_cmd_print.join(' ')}".yellow
  status = nil
  Open3.popen2(deploy_cmd.join(' ')) do |i, oe, thr|
    oe.each_line do |line|
      next if line.chomp == '' || line.chomp == '---'
      STDOUT.puts "#{Time.now} | #{line}"
      STDOUT.puts "---"
    end
    status = thr.value
  end
end

#generate_deploy_commandObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/faastruby/local/functions/function.rb', line 88

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', '--quiet']
  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_functionObject



119
120
121
122
123
# File 'lib/faastruby/local/functions/function.rb', line 119

def initialize_new_function
  debug "initialize_new_function"
  write_yaml
  write_handler
end

#languageObject



79
80
81
82
83
84
85
86
# File 'lib/faastruby/local/functions/function.rb', line 79

def language
  case YAML.load(File.read("#{@absolute_folder}/faastruby.yml"))['runtime']
  when /^ruby:/
    "ruby"
  when /^crystal:/
    "crystal"
  end
end

#load_yamlObject



131
132
133
134
# File 'lib/faastruby/local/functions/function.rb', line 131

def load_yaml
  debug "load_yaml"
  YAML.load(File.read("#{@absolute_folder}/faastruby.yml"))
end

#merge_yaml(hash, yaml_file) ⇒ Object



125
126
127
128
129
# File 'lib/faastruby/local/functions/function.rb', line 125

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_workspaceObject



108
109
110
111
112
113
114
115
116
117
# File 'lib/faastruby/local/functions/function.rb', line 108

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_yamlObject



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/faastruby/local/functions/function.rb', line 136

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_commentsObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/faastruby/local/functions/function.rb', line 149

def yaml_comments
  [
    '## To run tests before each deploy, uncomment the line test_command below',
    '## and change the command if you are not using Rspec.',
    '# test_command: rspec',
    '',
    '## 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