Class: FullStack

Inherits:
Object
  • Object
show all
Defined in:
lib/full_stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, options) ⇒ FullStack

Returns a new instance of FullStack.



4
5
6
7
8
9
# File 'lib/full_stack.rb', line 4

def initialize(site, options)
  @site = site
  @options = options
  @stop = false
  @pids = []
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/full_stack.rb', line 2

def options
  @options
end

Instance Method Details

#authenticateObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/full_stack.rb', line 11

def authenticate
  req = Net::HTTP::Post.new("/api/local/v1/authorize")
  req.body = "username=#{CGI.escape(@site.username)}&password=#{CGI.escape(@site.password)}"
  res = VaeLocal.fetch_from_vaeplatform(@site.subdomain, req)
  data = JSON.parse(res.body)
  if data['valid'] == "valid"
    FileUtils.mkdir_p(@site.data_path)
    if !File.exists?(@site.data_path + "/assets/")
      FileUtils.ln_s("#{vae_remote_path}/public", @site.data_path + "/assets")
    end
    @site.secret_key = data['secret_key']
    generation = File.exists?("#{@site.data_path}feed_generation") ? File.open("#{@site.data_path}feed_generation").read.to_i : 0
    if data['feed_url'] and data['feed_generation'].to_i > generation
      puts "Downloading updated Site Data Feed..."
      if curl = File.which("curl")
        `curl -o #{Shellwords.shellescape(@site.data_path)}feed.xml #{Shellwords.shellescape(data['feed_url'])}`
      else
        download_feed(data['feed_url'])
      end
      File.open("#{@site.data_path}feed_generation",'w') { |f| f.write(data['feed_generation']) }
    end
    File.open("#{@site.data_path}settings.php",'w') { |f| f.write(data['settings']) }
  else
    raise VaeError, "Error Connecting to Vae with the supplied Username and Password.  Please make sure this user has Vae Local permissions assigned."
  end
rescue JSON::ParserError
  raise VaeError, "An unknown error occurred signing into Vae Platform.  Please email support for help."
end

#brew_messageObject



128
129
130
# File 'lib/full_stack.rb', line 128

def brew_message
  "\n\nTo install Vae Local Full Stack dependencies on macOS via Homebrew, run the following commands:\n  brew tap actionverb/tap\n  brew install vae-frontend-compiler-service vae-remote vaedb vaeql\n\nMake sure you resolve any errors from Homebrew before proceeding."
end

#download_feed(url) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/full_stack.rb', line 40

def download_feed(url)
  url_base = url.split('/')[2]
  url_path = '/'+url.split('/')[3..-1].join('/')
  Net::HTTP.start(url_base) { |http|
    File.open("#{@site.data_path}feed.xml", 'w') { |f|
      http.get(URI.escape(url_path)) { |str|
        f.write str
      }
    }
  }
end

#find_dependency_project(folder, main_file, name) ⇒ Object

Raises:



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/full_stack.rb', line 116

def find_dependency_project(folder, main_file, name)
  thisdir = File.dirname(__FILE__)
  paths = [ "#{thisdir}/../../#{folder}", "#{thisdir}/../../../#{folder}", "/usr/local/#{folder}", "/usr/local/opt/#{folder}", "/usr/local/Cellar/#{folder}/1.0.0", "~/#{folder}" ]
  paths << "#{vae_remote_path}/tests/dependencies/#{folder}" if folder != "vae_remote"
  paths.each { |path|
    if File.exists?(path) and File.exists?("#{path}/#{main_file}")
      return path
    end
  }
  raise VaeError, "Could not find #{name} on your system.#{brew_message}"
end

#launch_daemonsObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/full_stack.rb', line 61

def launch_daemons
  if VaeLocal.port_open?(9092)
    puts "Starting Vae Frontend Compiler Service using Installation at #{vae_fcs_path}"
    @pids << fork {
      Dir.chdir(vae_fcs_path)
      STDOUT.reopen("/dev/null", "w")
      STDERR.reopen("/dev/null", "w")
      exec "bundle exec ./vaefcs.rb"
    }
  end
  port = 9100
  serve_root = @site.root
  loop {
    break if VaeLocal.port_open?(port)
    port = port + 1
  }
  if File.exists?(@site.root + "/.jekyll")
    serve_root = @site.root + "/_site/"
    FileUtils.mkdir_p(serve_root)
    @pids << fork {
      exec "bundle exec jekyll build --watch --source #{Shellwords.shellescape(@site.root)} --destination #{Shellwords.shellescape(serve_root)}"
    }
  end
  ENV['VAE_LOCAL_VAEDB_PORT'] = port.to_s
  @pids << fork {
    Dir.chdir(vaedb_path)
    puts "Starting Vaedb on Port #{port} using Installation at #{vaedb_path}"
    exec "./vaedb --port #{port} --busaddress 'tcp://*:#{port-4000}' --test --log_level #{options[:log_level]}"
  }
  @pids << fork {
    Dir.chdir(serve_root)
    ENV['VAE_LOCAL_BACKSTAGE'] = @site.subdomain + ".vaeplatform." + (ENV['VAEPLATFORM_LOCAL'] ? "dev" : "com")
    ENV['VAE_LOCAL_SECRET_KEY'] = @site.secret_key
    ENV['VAE_LOCAL_DATA_PATH'] = @site.data_path
    if options[:php_runtime] == "hhvm"
      ENV['VAE_LOCAL_HHVM'] = "1"
      exec "hhvm -c #{vae_remote_path}/tests/dependencies/php.ini -m server -d hhvm.pid_file=/tmp/vae_local.hhvm.pid -d auto_prepend_file=#{vae_remote_path}/lib/index.php -d hhvm.server.default_document=#{vae_remote_path}/lib/index.php -d hhvm.server.error_document404=#{vae_remote_path}/lib/index.php -d hhvm.server.port=#{options[:port]} -d hhvm.server.source_root=#{Shellwords.shellescape(@site.root)}"
    else
      exec "php -c #{vae_remote_path}/tests/dependencies/php.ini -S 0.0.0.0:#{options[:port]} #{vae_remote_path}/lib/index.php"
    end
  }
end

#runObject



52
53
54
55
56
57
58
59
# File 'lib/full_stack.rb', line 52

def run
  authenticate
  launch_daemons
  trap("INT") { @stop = true }
  loop { break if @stop; sleep 0.5 }
  puts "Quit signal received, cleaning up ..."
  @pids.map { |pid| Process.kill("TERM", pid) }
end

#vae_fcs_pathObject



108
109
110
# File 'lib/full_stack.rb', line 108

def vae_fcs_path
  @vae_fcs_path ||= find_dependency_project("vae-frontend-compiler-service", "vaefcs.rb", "Vae Frontend Compiler Service")
end

#vae_remote_pathObject



112
113
114
# File 'lib/full_stack.rb', line 112

def vae_remote_path
  @vae_remote_path ||= find_dependency_project("vae_remote", "lib/general.php", "Vae Remote")
end

#vaedb_pathObject



104
105
106
# File 'lib/full_stack.rb', line 104

def vaedb_path
  @vaedb_path ||= find_dependency_project("vaedb", "vaedb", "Vaedb")
end