Class: CapsuleCD::Node::NodeEngine

Inherits:
Engine
  • Object
show all
Defined in:
lib/capsulecd/node/node_engine.rb

Instance Attribute Summary

Attributes inherited from Engine

#config

Instance Method Summary collapse

Methods inherited from Engine

#initialize, #post_build_step, #post_package_step, #post_release_step, #post_runner_retrieve_payload, #post_source_configure, #post_source_process_pull_request_payload, #post_source_process_push_payload, #post_source_release, #post_test_step, #pre_build_step, #pre_package_step, #pre_release_step, #pre_runner_retrieve_payload, #pre_source_configure, #pre_source_process_pull_request_payload, #pre_source_process_push_payload, #pre_source_release, #pre_test_step, #start

Constructor Details

This class inherits a constructor from CapsuleCD::Engine

Instance Method Details

#build_stepObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/capsulecd/node/node_engine.rb', line 9

def build_step
  super
  # validate that the chef metadata.rb file exists
  unless File.exist?(@source_git_local_path + '/package.json')
    fail CapsuleCD::Error::BuildPackageInvalid, 'package.json file is required to process Npm package'
  end

  # no need to bump up the version here. It will automatically be bumped up via the npm version patch command.
  # however we need to read the version from the package.json file and check if a npm module already exists.

  # TODO: check if this module name and version already exist.

  # check for/create any required missing folders/files
  unless File.exist?(@source_git_local_path + '/test')
    FileUtils.mkdir(@source_git_local_path + '/test')
  end
  unless File.exist?(@source_git_local_path + '/.gitignore')
    CapsuleCD::GitUtils.create_gitignore(@source_git_local_path, ['Node'])
  end
end

#package_stepObject

run npm publish



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/capsulecd/node/node_engine.rb', line 84

def package_step
  super

  # commit changes to the cookbook. (test run occurs before this, and it should clean up any instrumentation files, created,
  # as they will be included in the commmit and any release artifacts)
  CapsuleCD::GitUtils.commit(@source_git_local_path, 'Committing automated changes before packaging.') rescue puts 'No changes to commit..'

  # run npm publish
  Open3.popen3("npm version #{@config.engine_version_bump_type} -m '(v%s) Automated packaging of release by CapsuleCD'", chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
    { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
      Thread.new do
        until (line = stream_buffer.gets).nil?
          puts "#{name} -> #{line}"
        end
      end
    end
    # wait for process
    external.join
    fail 'npm version bump failed' unless external.value.success?
  end

  @source_release_commit = CapsuleCD::GitUtils.get_latest_tag_commit(@source_git_local_path)
end

#release_stepObject

this step should push the release to the package repository (ie. npm, chef supermarket, rubygems)



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/capsulecd/node/node_engine.rb', line 109

def release_step
  super
  npmrc_path = File.expand_path('~/.npmrc')

  unless @config.npm_auth_token
    fail CapsuleCD::Error::ReleaseCredentialsMissing, 'cannot deploy page to npm, credentials missing'
  end

  # write the knife.rb config file.
  File.open(npmrc_path, 'w+') do |file|
    file.write("//registry.npmjs.org/:_authToken=#{@config.npm_auth_token}")
  end

  # run npm publish
  Open3.popen3('npm publish .', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
    { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
      Thread.new do
        until (line = stream_buffer.gets).nil?
          puts "#{name} -> #{line}"
        end
      end
    end
    # wait for process
    external.join
    unless external.value.success?
      fail CapsuleCD::Error::ReleasePackageError, 'npm publish failed. Check log for exact error'
    end
  end
end

#test_stepObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/capsulecd/node/node_engine.rb', line 30

def test_step
  super

  # the module has already been downloaded. lets make sure all its dependencies are available.
  Open3.popen3('npm install', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
    { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
      Thread.new do
        until (line = stream_buffer.gets).nil?
          puts "#{name} -> #{line}"
        end
      end
    end
    # wait for process
    external.join
    unless external.value.success?
      fail CapsuleCD::Error::TestDependenciesError, 'npm install failed. Check module dependencies'
    end
  end

  # create a shrinkwrap file.
  Open3.popen3('npm shrinkwrap', chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
    { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
      Thread.new do
        until (line = stream_buffer.gets).nil?
          puts "#{name} -> #{line}"
        end
      end
    end
    # wait for process
    external.join
    unless external.value.success?
      fail CapsuleCD::Error::TestDependenciesError, 'npm shrinkwrap failed. Check log for exact error'
    end
  end

  # run test command
  test_cmd = @config.engine_cmd_test || 'npm test'
  Open3.popen3(ENV, test_cmd, chdir: @source_git_local_path) do |_stdin, stdout, stderr, external|
    { stdout: stdout, stderr: stderr }. each do |name, stream_buffer|
      Thread.new do
        until (line = stream_buffer.gets).nil?
          puts "#{name} -> #{line}"
        end
      end
    end
    # wait for process
    external.join
    unless external.value.success?
      fail CapsuleCD::Error::TestRunnerError, test_cmd + ' failed. Check log for exact error'
    end
  end unless @config.engine_disable_test
end