Class: Iota::Function
- Inherits:
-
Object
- Object
- Iota::Function
- Defined in:
- lib/iota/function.rb
Constant Summary collapse
- DEFAULT_HANDLER =
{ 'nodejs' => { handler_name: 'index.handler', file_name: 'index.js' }, 'python2.7' => { handler_name: 'lambda_function.lambda_handler', file_name: 'lambda_function.py' } }
- RUNTIMES =
DEFAULT_HANDLER.keys
- ENVIRONMENT_ALIAS_NAME_MAP =
{ production: 'PROD', development: 'DEV' }
- PROD_VERSION_ALIAS_NAME =
TODO: multiple last_versions, multiple rollback
ENVIRONMENT_ALIAS_NAME_MAP[:production]
- DEV_VERSION_ALIAS_NAME =
ENVIRONMENT_ALIAS_NAME_MAP[:development]
- LAST_VERSION_SUFFIX =
'_LAST'- IGNORES =
%w( node_modules bin src test iota_conf.rb )
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
- #create_alias(alias_name, version = '$LATEST') ⇒ Object
- #create_function(runtime, role) ⇒ Object
- #create_local(runtime) ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(name, path) ⇒ Function
constructor
A new instance of Function.
-
#list_aliases ⇒ Object
list aliases.
-
#list_versions ⇒ Object
list versions.
- #publish_version ⇒ Object
- #remote ⇒ Object
- #update_alias(alias_, target_version) ⇒ Object
- #update_code ⇒ Object
Constructor Details
#initialize(name, path) ⇒ Function
Returns a new instance of Function.
42 43 44 45 |
# File 'lib/iota/function.rb', line 42 def initialize(name, path) @name = name @path = path end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
40 41 42 |
# File 'lib/iota/function.rb', line 40 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
40 41 42 |
# File 'lib/iota/function.rb', line 40 def path @path end |
Instance Method Details
#create_alias(alias_name, version = '$LATEST') ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/iota/function.rb', line 143 def create_alias(alias_name, version='$LATEST') client.create_alias({ function_name: name, name: alias_name, function_version: version }) end |
#create_function(runtime, role) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/iota/function.rb', line 47 def create_function(runtime, role) if RUNTIMES.include? runtime Dir.mktmpdir do |tmpdir| zip_file_path = package_function(tmpdir) handler = DEFAULT_HANDLER[runtime] client.create_function({ function_name: name, runtime: runtime, handler: handler[:handler_name], role: role || ENV['AWS_LAMBDA_IAM_ROLE'], code: { zip_file: File.open(zip_file_path, "rb") } }) end end end |
#create_local(runtime) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/iota/function.rb', line 67 def create_local(runtime) Dir.mkdir path handler = DEFAULT_HANDLER[runtime] # write function File.open(File.join(path, handler[:file_name]), "w") do |file| template = File.read(File.join(templates_path, template_file_name[runtime])) template_code = ERB.new(template).result(binding) file.write(template_code) end # write config # TODO: 'iota.conf' is hard-coded File.open(File.join(path, 'iota.conf'), "w") do |file| role = ENV['AWS_LAMBDA_IAM_ROLE'] || '' template = File.read(File.join(templates_path, template_file_name['config'])) template_config = ERB.new(template).result(binding) file.write(template_config) end path end |
#exists? ⇒ Boolean
104 105 106 107 108 109 110 |
# File 'lib/iota/function.rb', line 104 def exists? begin !!remote rescue Aws::Lambda::Errors::ResourceNotFoundException false end end |
#list_aliases ⇒ Object
list aliases
98 99 100 101 102 |
# File 'lib/iota/function.rb', line 98 def list_aliases client.list_aliases({ function_name: name }).aliases end |
#list_versions ⇒ Object
list versions
91 92 93 94 95 |
# File 'lib/iota/function.rb', line 91 def list_versions client.list_versions_by_function({ function_name: name }).versions end |
#publish_version ⇒ Object
137 138 139 140 141 |
# File 'lib/iota/function.rb', line 137 def publish_version client.publish_version({ function_name: name }) end |
#remote ⇒ Object
112 113 114 115 116 |
# File 'lib/iota/function.rb', line 112 def remote client.get_function({ function_name: name }) end |
#update_alias(alias_, target_version) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/iota/function.rb', line 118 def update_alias(alias_, target_version) client.update_alias({ function_name: name, name: alias_, function_version: target_version }) end |
#update_code ⇒ Object
126 127 128 129 130 131 132 133 134 135 |
# File 'lib/iota/function.rb', line 126 def update_code Dir.mktmpdir do |tmpdir| zip_file_path = package_function(tmpdir) resp = client.update_function_code( function_name: name, zip_file: File.open(zip_file_path, "rb"), ) return resp end end |