Class: Ploy::LocalPackage::DebBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ploy/localpackage/debbuilder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DebBuilder

Returns a new instance of DebBuilder.



18
19
20
21
# File 'lib/ploy/localpackage/debbuilder.rb', line 18

def initialize(opts = {})
  @metadata_dir = "/etc/ploy/metadata.d"
  opts.each { |k,v| instance_variable_set("@#{k}", v) } # maybe?
end

Instance Attribute Details

#branchObject

Returns the value of attribute branch.



10
11
12
# File 'lib/ploy/localpackage/debbuilder.rb', line 10

def branch
  @branch
end

#dist_dirObject

Returns the value of attribute dist_dir.



14
15
16
# File 'lib/ploy/localpackage/debbuilder.rb', line 14

def dist_dir
  @dist_dir
end

#dist_dirsObject

Returns the value of attribute dist_dirs.



13
14
15
# File 'lib/ploy/localpackage/debbuilder.rb', line 13

def dist_dirs
  @dist_dirs
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/ploy/localpackage/debbuilder.rb', line 8

def name
  @name
end

#postinstObject

Returns the value of attribute postinst.



16
17
18
# File 'lib/ploy/localpackage/debbuilder.rb', line 16

def postinst
  @postinst
end

#prefixObject

Returns the value of attribute prefix.



15
16
17
# File 'lib/ploy/localpackage/debbuilder.rb', line 15

def prefix
  @prefix
end

#shaObject

Returns the value of attribute sha.



9
10
11
# File 'lib/ploy/localpackage/debbuilder.rb', line 9

def sha
  @sha
end

#timestampObject

Returns the value of attribute timestamp.



11
12
13
# File 'lib/ploy/localpackage/debbuilder.rb', line 11

def timestamp
  @timestamp
end

#upstart_filesObject

Returns the value of attribute upstart_files.



12
13
14
# File 'lib/ploy/localpackage/debbuilder.rb', line 12

def upstart_files
  @upstart_files
end

Instance Method Details

#build_debObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ploy/localpackage/debbuilder.rb', line 23

def build_deb
  info = nil
  Dir.mktmpdir do |dir|
    dist_dirs = Array.new(@dist_dirs || [])
    if @dist_dir
      dist_dirs.unshift({'dir' => @dist_dir, 'prefix' => @prefix})
    end
    mirror_dists(dir, dist_dirs)
    #mirror_dist(dir, @prefix, @dist_dir)
    (dir)
    Tempfile.open(['postinst', 'sh']) do |file|
      write_after_install_script(file)
      ol = fpm_optlist(dir)
      ol.add("--after-install", file.path)
      #puts "debug: fpm #{ol.as_string} ."
      info = eval(`fpm #{ol.as_string} .`)
    end
  end
  return info[:path]
end

#fpm_optlist(dir) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ploy/localpackage/debbuilder.rb', line 44

def fpm_optlist(dir)
  optlist = Ploy::LocalPackage::DebBuilderOptlist.new [
    { "-n" => @name },
    { "-s" => "dir" },
    { "-t" => "deb" },
    { "-a" => "all" },
    { "-C" => dir },
    { "--deb-field" => "'gitrev: #{@sha}'" },
    "-f",
    "--deb-no-default-config-files",
    { "-v" => safeversion(@timestamp + '.' + @branch) },
  ]

  if @upstart_files then
    @upstart_files.each do | upstart |
      optlist.add("--deb-upstart", upstart)
    end
  end

  return optlist
end

#mirror_dist(topdir, prefix, source_dir) ⇒ Object



76
77
78
79
# File 'lib/ploy/localpackage/debbuilder.rb', line 76

def mirror_dist(topdir, prefix, source_dir)
  FileUtils.mkpath mirror_dist_target(topdir, prefix)
  system("rsync -a #{source_dir}/* #{mirror_dist_target(topdir, prefix)}")
end

#mirror_dist_target(topdir, prefix) ⇒ Object



81
82
83
# File 'lib/ploy/localpackage/debbuilder.rb', line 81

def mirror_dist_target(topdir, prefix)
  return prefix ? File.join(topdir, prefix) : topdir
end

#mirror_dists(topdir, dist_dirs) ⇒ Object



70
71
72
73
74
# File 'lib/ploy/localpackage/debbuilder.rb', line 70

def mirror_dists(topdir, dist_dirs)
  dist_dirs.each do |source|
    mirror_dist(topdir, source['prefix'], source['dir'])
  end
end

#safeversion(txt) ⇒ Object



66
67
68
# File 'lib/ploy/localpackage/debbuilder.rb', line 66

def safeversion(txt)
  return txt.gsub(/[^A-Za-z0-9\.\+]/, '')
end

#write_after_install_script(file) ⇒ Object



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
# File 'lib/ploy/localpackage/debbuilder.rb', line 85

def write_after_install_script(file)
  file.write <<SCRIPT
#!/bin/bash
# BEGIN PACKAGE POSTINST
#{postinst}
# END PACKAGE POSTINST
SCRIPT

  if @upstart_files then
    @upstart_files.each do | upstart |
      service = File.basename(upstart)
      file.write <<SCRIPT
# Everything below added by ploy automatically...
# this is awful
check_upstart_service(){
  status $1 | grep -q "^$1 start" > /dev/null
  return $?
}

if check_upstart_service #{service}; then
  stop #{service}
fi
start #{service}
SCRIPT
    end
  end
  file.flush
end

#write_metadata(dir) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ploy/localpackage/debbuilder.rb', line 114

def (dir)
  base = File.join(dir, @metadata_dir)
  FileUtils.mkpath(base)
  path = File.join(base, "#{@name}.yml")
  info = {
    "name"      => @name,
    "sha"       => @sha,
    "timestamp" => @timestamp,
    "branch"    => @branch, 
  }
  File.open(path, 'w') do | out |
    YAML.dump(info, out)
  end

end