Module: FaaStRuby::Local

Extended by:
Logger
Defined in:
lib/faastruby/local.rb,
lib/faastruby/local/logger.rb,
lib/faastruby/server/local.rb,
lib/faastruby/local/functions.rb,
lib/faastruby/local/listeners.rb,
lib/faastruby/local/processors.rb,
lib/faastruby/local/static_files.rb,
lib/faastruby/local/functions/ruby.rb,
lib/faastruby/local/functions/crystal.rb,
lib/faastruby/local/functions/function.rb,
lib/faastruby/local/listeners/listener.rb,
lib/faastruby/local/processors/function.rb,
lib/faastruby/local/processors/processor.rb,
lib/faastruby/local/processors/static_file.rb,
lib/faastruby/local/static_files/static_file.rb

Defined Under Namespace

Modules: Logger Classes: CrystalFunction, Function, FunctionProcessor, Listener, ListenerEvent, MissingConfigurationFileError, Processor, RubyFunction, StaticFile, StaticFileProcessor

Constant Summary collapse

DEBUG =
ENV['DEBUG']
CRYSTAL_ENABLED =
crystal_present_and_supported?
RUBY_ENABLED =
ruby_present_and_supported?
SERVER_ROOT =
Dir.pwd
PROJECT_YAML_FILE =
"#{SERVER_ROOT}/project.yml"
SECRETS_YAML_FILE =
"#{SERVER_ROOT}/secrets.yml"
DEPLOY_ENVIRONMENT =
ENV['DEPLOY_ENVIRONMENT'] || 'stage'
SYNC_ENABLED =
ENV['SYNC'] && check_if_logged_in
CRYSTAL_VERSION =
get_crystal_version.freeze
DEFAULT_CRYSTAL_RUNTIME =
"crystal:#{CRYSTAL_VERSION}".freeze
DEFAULT_RUBY_RUNTIME =
"ruby:#{RUBY_VERSION}".freeze
FUNCTIONS_EVENT_QUEUE =
Queue.new
PUBLIC_EVENT_QUEUE =
Queue.new
STDOUT_MUTEX =
Mutex.new

Class Method Summary collapse

Methods included from Logger

debug, print, puts, puts

Class Method Details

.catch_allObject



93
94
95
96
# File 'lib/faastruby/local.rb', line 93

def self.catch_all
  debug "self.catch_all"
  project_config['catch_all'] || 'catch-all'
end

.check_if_logged_inObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/faastruby/local.rb', line 36

def self.check_if_logged_in
  creds_file = File.expand_path("~/.faastruby/credentials.yml")
  yaml = YAML.load(File.read(creds_file)) rescue {}
  unless yaml['credentials'] && yaml['credentials']['email']
    STDOUT.puts "@@@ WARNING @@@ | You need to be logged in to use FaaStRuby Local with sync mode.".red
    STDOUT.puts "@@@ WARNING @@@ | To login, run: faastruby login".red
    STDOUT.puts "@@@ WARNING @@@ | Sync mode is *NOT* enabled!".red
    STDOUT.puts "---".red
    return false
  end
  return true
end

.crystal_present_and_supported?Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/faastruby/local.rb', line 26

def self.crystal_present_and_supported?
  debug "self.crystal_present_and_supported?"
  system("which crystal >/dev/null") && SUPPORTED_CRYSTAL.include?(get_crystal_version)
end

.deploy_environment_secretsObject



118
119
120
121
# File 'lib/faastruby/local.rb', line 118

def self.deploy_environment_secrets
  debug "self.deploy_environment_secrets"
  secrets[DEPLOY_ENVIRONMENT] || {}
end

.functionsObject



98
99
100
101
# File 'lib/faastruby/local.rb', line 98

def self.functions
  debug "self.functions"
  @@functions ||= []
end

.functions_dirObject



78
79
80
81
# File 'lib/faastruby/local.rb', line 78

def self.functions_dir
  debug "self.functions_dir"
  "#{SERVER_ROOT}/#{project_config['functions_dir'] || 'functions'}"
end

.functions_listenerObject



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

def self.functions_listener
  debug "self.functions_listener"
  @@functions_listener
end

.get_crystal_versionObject



20
21
22
23
24
# File 'lib/faastruby/local.rb', line 20

def self.get_crystal_version
  debug "self.get_crystal_version"
  ver = `crystal -v|head -n1|cut -f2 -d' ' 2>/dev/null`&.chomp
  ver == '' ? CRYSTAL_LATEST : ver
end

.initial_compile_and_deploy(crystal_functions) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/faastruby/local.rb', line 147

def self.initial_compile_and_deploy(crystal_functions)
  debug "initial_compile_and_deploy(#{crystal_functions.inspect})"
  puts "Triggering 'compile' on Crystal functions." if crystal_functions.any?
  if SYNC_ENABLED
    puts "Running initial cloud sync."
    StaticFile.full_sync
  end
  sleep 1
  @@functions.each {|f| FileUtils.touch("#{f.absolute_folder}/faastruby.yml")}
end

.listen_on_functions_dirObject



174
175
176
177
178
179
# File 'lib/faastruby/local.rb', line 174

def self.listen_on_functions_dir
  debug "self.listen_on_functions_dir"
  debug "Listening for changes in '#{functions_dir}'"
  @@functions_listener = Listener.new(directory: functions_dir, queue: FUNCTIONS_EVENT_QUEUE)
  @@functions_listener.start
end

.listen_on_public_dirObject



181
182
183
184
185
186
# File 'lib/faastruby/local.rb', line 181

def self.listen_on_public_dir
  debug "self.listen_on_public_dir"
  debug "Listening for changes in '#{public_dir}'"
  @@public_listener = Listener.new(directory: public_dir, queue: PUBLIC_EVENT_QUEUE)
  @@public_listener.start
end

.project_configObject



74
75
76
77
# File 'lib/faastruby/local.rb', line 74

def self.project_config
  debug "self.project_config"
  yaml = YAML.load(File.read(PROJECT_YAML_FILE))['project']
end

.public_dirObject



83
84
85
86
# File 'lib/faastruby/local.rb', line 83

def self.public_dir
  debug "self.public_dir"
  "#{SERVER_ROOT}/#{project_config['public_dir'] || 'public'}"
end

.public_listenerObject



108
109
110
111
# File 'lib/faastruby/local.rb', line 108

def self.public_listener
  debug "self.public_listener"
  @@public_listener
end

.root_toObject



88
89
90
91
# File 'lib/faastruby/local.rb', line 88

def self.root_to
  debug "self.root_to"
  project_config['root_to'] || 'root'
end

.ruby_present_and_supported?Boolean

Returns:

  • (Boolean)


31
32
33
34
# File 'lib/faastruby/local.rb', line 31

def self.ruby_present_and_supported?
  debug "self.ruby_present_and_supported?"
  system("which ruby >/dev/null") && SUPPORTED_RUBY.include?(RUBY_VERSION)
end

.secretsObject



123
124
125
126
# File 'lib/faastruby/local.rb', line 123

def self.secrets
  debug "self.secrets"
  YAML.load(File.read(SECRETS_YAML_FILE))['secrets'] || {}
end

.secrets_for_function(function_name) ⇒ Object



113
114
115
116
# File 'lib/faastruby/local.rb', line 113

def self.secrets_for_function(function_name)
  debug "self.secrets_for_function(#{function_name.inspect})"
  deploy_environment_secrets[function_name] || {}
end

.start!(sync: false, deploy_env: 'stage', debug: false) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/faastruby/local.rb', line 128

def self.start!
  Listen::Adapter::Linux::DEFAULTS[:events] << :modify
  debug "self.start!"
  sync_mode_enabled if SYNC_ENABLED
  @@functions = Function.find_all_in(functions_dir)
  ruby_functions = @@functions.map{|f| f.name if f.language == "ruby"}.compact
  crystal_functions = @@functions.map{|f| f.name if f.language == "crystal"}.compact
  puts "Detecting existing functions."
  puts "Ruby functions: #{ruby_functions.inspect}"
  puts "Crystal functions: #{crystal_functions.inspect}"
  listen_on_functions_dir
  listen_on_public_dir if SYNC_ENABLED
  initial_compile_and_deploy(crystal_functions)
  puts "Listening for changes."
  FunctionProcessor.new(FUNCTIONS_EVENT_QUEUE).start
  StaticFileProcessor.new(PUBLIC_EVENT_QUEUE).start if SYNC_ENABLED
  sleep
end

.sync_mode_enabledObject



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/faastruby/local.rb', line 158

def self.sync_mode_enabled
  debug __method__
  puts "Sync mode enabled."
  print "Connecting to workspace '#{workspace}'... "
  try_to_create = Proc.new {system("faastruby create-workspace #{workspace}")}
  has_credentials = system("faastruby list-workspace #{workspace} > /dev/null 2>&1")
  continue = has_credentials || try_to_create.call
  unless continue
    puts "[FATAL] Unable to setup project workspace '#{workspace}'. 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
    exit 1
  end
  STDOUT.puts "connected!\n".yellow
  puts "Your local environment will be synced to https://#{workspace}.tor1.faast.cloud"
  true
end

.workspaceObject



68
69
70
71
72
# File 'lib/faastruby/local.rb', line 68

def self.workspace
  debug "self.workspace"
  return "#{project_config['name']}-#{DEPLOY_ENVIRONMENT}-#{project_config['identifier']}" if project_config['identifier']
  "#{project_config['name']}-#{DEPLOY_ENVIRONMENT}"
end