Class: Shaddox::Shadow

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

Instance Method Summary collapse

Constructor Details

#initialize(options, &block) ⇒ Shadow

include FileUtils



12
13
14
15
16
17
18
# File 'lib/shaddox/shadow.rb', line 12

def initialize(options, &block)
  @debug = options[:debug] || true # TODO: change to false for actual release
  @installer = options[:installer]
  @tmppath = options[:tmppath] || '/tmp/shaddox/'
  @required = true
  instance_eval(&block)
end

Instance Method Details

#cd(path, &block) ⇒ Object



38
39
40
41
42
43
# File 'lib/shaddox/shadow.rb', line 38

def cd(path, &block)
  mkdir(path)
  FileUtils.cd(path.exp_path) do
    instance_eval(&block)
  end
end

#ensure_gitObject



74
75
76
77
78
79
# File 'lib/shaddox/shadow.rb', line 74

def ensure_git()
  unless @git_installed
    install 'git'
    @git_installed = true
  end
end

#ensure_parent_dir(path) ⇒ Object



69
70
71
72
# File 'lib/shaddox/shadow.rb', line 69

def ensure_parent_dir(path)
  dir, base = File.split(path.exp_path)
  mkdir(dir)
end

#exec(command, opts = {:verbose => false}) ⇒ Object



32
33
34
35
36
# File 'lib/shaddox/shadow.rb', line 32

def exec(command, opts = {:verbose => false})
  info "Running '#{command}' in '#{Dir.pwd}'", 1 if opts[:verbose] or @debug
  system(command)
  raise "#{line} failed" unless $? == 0 or !@required
end

#exists(path) ⇒ Object



45
46
47
# File 'lib/shaddox/shadow.rb', line 45

def exists(path)
  system("test -e #{path.exp_path}")
end

#exists_d(path) ⇒ Object



49
50
51
# File 'lib/shaddox/shadow.rb', line 49

def exists_d(path)
  system("test -d #{path.exp_path}")
end

#exists_f(path) ⇒ Object



53
54
55
# File 'lib/shaddox/shadow.rb', line 53

def exists_f(path)
  system("test -f #{path.exp_path}")
end

#install(package) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/shaddox/shadow.rb', line 127

def install(package)
  unless @installer
    # TODO: Try to autodetect package manager
    warn "No installer is specified for this target.", 1
    puts "-------------------"
    require 'highline/import'
    choose do |menu|
      menu.prompt = "Please select a package manager to use:"

      menu.choice(:apt) { @installer = :apt }
      menu.choice(:brew) { @installer = :brew }
    end
    puts "-------------------"
  end
  raise "No installer specified for this target!" unless @installer
  info "Ensuring #{package} is installed with #{@installer}", 1 if @debug
  package_installed = lambda { system("type #{package} >/dev/null 2>&1") }
  unless package_installed.call()
    case @installer
    when :apt
      exec "sudo apt-get install -y #{package}"
    when :brew
      exec "brew install #{package}"
    end
  end
  raise "#{package} could not be installed." unless package_installed.call()
end

#ln_s(source, dest, opts = {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/shaddox/shadow.rb', line 57

def ln_s(source, dest, opts = {})
  ensure_parent_dir(source)
  ensure_parent_dir(dest)
  info "Linking '#{source.exp_path}' to '#{dest.exp_path}'", 1 if @debug
  FileUtils::ln_s(source.exp_path, dest.exp_path, opts)
end

#mkdir(path) ⇒ Object



64
65
66
67
# File 'lib/shaddox/shadow.rb', line 64

def mkdir(path)
  info "Ensuring directory '#{path}' exists", 1 if @debug
  FileUtils::mkdir_p(path.exp_path)
end

#optional(&block) ⇒ Object



20
21
22
23
24
# File 'lib/shaddox/shadow.rb', line 20

def optional(&block)
  @required = false
  instance_eval(&block)
  @required = true
end

#repo_deploy(repo_key, deploy_path, opts = {}) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/shaddox/shadow.rb', line 81

def repo_deploy(repo_key, deploy_path, opts ={})
  keep_releases = opts[:keep_releases] || 5
  repo = @repos[repo_key]

  ensure_git()
  ensure_parent_dir(deploy_path)
  deploy_path = deploy_path.exp_path

  cd deploy_path do

    # Get the current release number
    release = 0
    cd 'releases' do
      current_max = Dir.entries('.').select { |e| e =~ /\d+/ }.max
      release = current_max.to_i + 1 if current_max
    end

    # Make a new release dir
    release_path = "./releases/#{release}"

    case repo.vcs
    when :git
      # Clone/update repo in vcs:
      info 'Updating the repository', 1 if @debug
      if exists_d('vcs')
        cd 'vcs' do
          exec "git fetch #{repo.url} #{repo.branch}:#{repo.branch} --force"
        end
      else
        exec "git clone #{repo.url} vcs --bare"
      end
      exec "git clone ./vcs #{release_path} --recursive --branch #{repo.branch}"
    end

    # Link shared paths
    info 'Linking shared paths', 1 if @debug
    repo.shared.each do |shared_path|
      ln_s "#{release_path}/#{shared_path}", "./shared/#{shared_path}"
    end

    # Link ./current to the latest release
    info 'Linking current to latest release', 1 if @debug
    ln_s './current', release_path
  end
end

#sh(command, args = nil) ⇒ Object



26
27
28
29
30
# File 'lib/shaddox/shadow.rb', line 26

def sh(command, args = nil)
  line = "#{command}"
  line += " #{args.join(" ")}" if args
  exec(line, :verbose => true)
end