Class: EmDeploy::Deployment

Inherits:
Object
  • Object
show all
Defined in:
lib/em-deploy/deployment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(environment = 'development') ⇒ Deployment

Returns a new instance of Deployment.



58
59
60
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
# File 'lib/em-deploy/deployment.rb', line 58

def initialize(environment='development')
  puts %x(tput civis)
  
  json                  = JSON.parse( File.read('./deploy.json') )
  custom                = {}
  options               = {}
  options[:aws]         = { region: 'us-east-1' }
  options[:redis]       = { host: '127.0.0.1', port: 6379 }
  options[:environment] = environment

  if json[environment]
    json[environment].each do |k,v|
      if v.class == Hash
        custom[k.to_sym] = {}
        v.each { |_k,_v| custom[k.to_sym][_k.to_sym] = _v }
      else
        custom[k.to_sym] = v
      end
    end
  end

  self.config      = options.merge!(custom)
  self.revision    = %x(git rev-parse --short HEAD).chomp!
  self.app_name    = JSON.parse( File.read('./package.json') )['name'].downcase.chomp

  puts "Preparing to deploy #{app_name.capitalize}. Environment: #{environment} Revision: #{"#{revision}".colorize(:light_blue)}"
  %x(rm -rf #{}/dist) if File.directory?('./dist')
  build_application

  if File.directory?('./dist')
    upload_assets
    activate_revision
  end
  
  puts "\nDeployment Successful!".colorize(:light_green)
  puts %x(tput cnorm)
end

Instance Attribute Details

#app_nameObject

Returns the value of attribute app_name.



3
4
5
# File 'lib/em-deploy/deployment.rb', line 3

def app_name
  @app_name
end

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/em-deploy/deployment.rb', line 3

def config
  @config
end

#revisionObject

Returns the value of attribute revision.



3
4
5
# File 'lib/em-deploy/deployment.rb', line 3

def revision
  @revision
end

Instance Method Details

#activate_revisionObject



35
36
37
38
39
# File 'lib/em-deploy/deployment.rb', line 35

def activate_revision
  redis = Redis.new(config[:redis])
  redis.set(revision, File.read('./dist/index.html'))
  redis.set("#{app_name}:current", revision)
end

#build_applicationObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/em-deploy/deployment.rb', line 41

def build_application
  c = %w{ | / - \\ }
  t = Thread.new { %x(ember build --environment #{config[:environment]}) }
  
  print "\n"
  print "Building #{app_name.capitalize} "
  while t.alive?
    print c[0]
    sleep 0.1
    print "\b"
    c.push(c.shift)
  end
  t.join
  puts "\n"
  puts "\n"
end

#upload_assetsObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/em-deploy/deployment.rb', line 5

def upload_assets
  bar     = ProgressBar.create(title: 'Uploading Assets', progress_mark: '='.colorize(:light_blue), length: 80)
  count   = 0
  threads = []

  Dir.glob('./dist/assets/*') do |path|
    t = Thread.new do
      p = File.expand_path(path)
      if File.directory?(p)
        Dir.glob("#{p}/*") { |sub| Uploader.new(sub, config) }
      else
        Uploader.new(p, config)
      end
    end
    threads.push(t)
  end

  bar.total    = threads.count
  bar.progress = count
  
  while count < threads.count do
    count        = threads.map { |t| t unless t.alive? }.compact.count
    bar.progress = count
    sleep 0.2
  end
  
  threads.each(&:join)
  !threads.map { |t| t.alive? }.compact.include?(true)
end