Skip to content

Commit

Permalink
Add support for deployment groups (#31)
Browse files Browse the repository at this point in the history
* Add deployment groups support. Feature #27
  • Loading branch information
ivadim authored Aug 5, 2018
1 parent a5dab42 commit 1fbedb2
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ suites:
vsts_url: <%= ENV['VSTS_URL'] %>
vsts_pool: <%= ENV['VSTS_POOL'] %>
vsts_token: <%= ENV['VSTS_TOKEN'] %>
deployment_group_name: <%= ENV['VSTS_DEPLOYMENT_GROUP_NAME'] %>
deployment_group_project: <%= ENV['VSTS_DEPLOYMENT_GROUP_PROJECT'] %>

- name: linux-basic
run_list:
Expand All @@ -112,6 +114,8 @@ suites:
vsts_url: <%= ENV['VSTS_URL'] %>
vsts_pool: <%= ENV['VSTS_POOL'] %>
vsts_token: <%= ENV['VSTS_TOKEN'] %>
deployment_group_name: <%= ENV['VSTS_DEPLOYMENT_GROUP_NAME'] %>
deployment_group_project: <%= ENV['VSTS_DEPLOYMENT_GROUP_PROJECT'] %>

- name: osx-basic
run_list:
Expand Down
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ This resource installs and configures the vsts build and release agent
- `:restart`: Restart the agent service

#### Parameters
- `agent_name`: Name attribute. The name of the vsts agent
- `agent_name`: name attribute. The name of the vsts agent
- `deploymentGroup`: deploy the agent into the [deployment group](https://docs.microsoft.com/en-us/vsts/pipelines/release/deployment-groups/?view=vsts). Default '`false`'
* `deploymentGroupName`: name of the deployment group. Only applies if `deploymentGroup==true`
* `projectName`: name of the vsts/tfs project where to deploy the agent. Only applies if `deploymentGroup==true`
* `collectionName`: name of the vsts/tfs collection where to deploy the agent. Only applies if `deploymentGroup==true`. Dafault value is `DefaultCollection`
* `deploymentGroupTags`: a comma-separated list of tags to set for the agent. Only applies if `deploymentGroup==true`
- `version`: an agent version to install. Default version from an attribute
- `install_dir`: A target directory to install the vsts agent
- `path`: Overwrite system PATH environment variable values. Linux and macOS only
- `env`: Additional environment variables. Linux and macOS only
- `user`: Set a local user to run the vsts agent
- `group`: Set a local group to run the vsts agent
- `runasservice`: run agent as a service. Default 'true'
- `runasservice`: run agent as a service. Default '`true`'
- `windowslogonaccount`: Set a user name to run a windows service. Possible values are "NT AUTHORITY\NetworkService", "NT AUTHORITY\LocalService" or any system valid username
- `windowslogonpassword`: Set password for windowslogonaccount unless it is equal to NetworkService or LocalService
- `vsts_url`: url to VSTS instance
Expand All @@ -64,7 +69,7 @@ This resource installs and configures the vsts build and release agent
- `work_folder`: Set different workspace location. Default is "install_dir/\_work"

#### Examples
Install, configure, restart and remove an agent.
Install, configure, restart and remove a build agent.
Check [windows](test/cookbooks/windows-basic/recipes/default.rb), [linux](test/cookbooks/linux-basic/recipes/default.rb) or [osx](test/cookbooks/osx-basic/recipes/default.rb) tests for more examples.

```ruby
Expand Down Expand Up @@ -100,5 +105,28 @@ vsts_agent 'agent_01' do
end
```

Install, configure, restart and remove a deployment agent.

```ruby
vsts_agent 'deployment_agent_01' do
install_dir dir
deploymentGroup true
deploymentGroupName 'project1-deployment-group'
projectName 'project1'
collectionName 'DefaultCollection'
deploymentGroupTags "web, db"
user 'vagrant'
group 'vagrant'
path '/usr/local/bin/:/usr/bin:/opt/bin/' # only works on nix systems
env('M2_HOME' => '/opt/maven', 'JAVA_HOME' => '/opt/java') # only works on nix systems
vsts_url 'https://contoso.visualstudio.com'
vsts_token 'my_secret_token_from_vsts'
windowslogonaccount 'builder' # will be used only on windows
windowslogonpassword 'Pas$w0r_d' # will be used only on windows
action :install
end

```

# How to contribute
Check [Contribution Guide](CONTRIBUTING.md) and [Testing Guide](TESTING.md)
4 changes: 4 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ set VSTS_URL='https://account.visualstudio.com'
set VSTS_POOL=default
set VSTS_USER=username
set VSTS_TOKEN=my_secret_token_from_vsts
set VSTS_DEPLOYMENT_GROUP_NAME=test-chef-deployment
set VSTS_DEPLOYMENT_GROUP_PROJECT=test
```
Linux / Mac:
```
export VSTS_URL='https://account.visualstudio.com'
export VSTS_POOL=default
export VSTS_USER=username
export VSTS_TOKEN=my_secret_token_from_vsts
export VSTS_DEPLOYMENT_GROUP_NAME=test-chef-deployment
export VSTS_DEPLOYMENT_GROUP_PROJECT=test
```

If the username of the box you're testing differs from standard "vagrant":
Expand Down
2 changes: 1 addition & 1 deletion attributes/default.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
default['vsts_agent']['binary']['version'] = '2.126.0'
default['vsts_agent']['binary']['version'] = '2.136.1'

case node['platform_family']
when 'windows'
Expand Down
2 changes: 1 addition & 1 deletion metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
source_url 'https://github.com/Microsoft/vsts-agent-cookbook' if respond_to?(:source_url)
issues_url 'https://github.com/Microsoft/vsts-agent-cookbook/issues' if respond_to?(:issues_url)
chef_version '>= 12.4' if respond_to?(:chef_version)
version '2.0.4'
version '3.0.0'

%w(ubuntu debian redhat centos mac_os_x windows).each do |operating_system|
supports operating_system
Expand Down
30 changes: 28 additions & 2 deletions resources/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
property :vsts_password, String, sensitive: true, desired_state: false
property :vsts_token, String, sensitive: true, desired_state: false

# Deployment Groups
property :deploymentGroup, [TrueClass, FalseClass], default: false
property :deploymentGroupName, String
property :deploymentGroupTags, String, desired_state: false
property :projectName, String
property :collectionName, String, default: 'DefaultCollection'

include ::VSTS::Agent::Helpers

load_current_value do
Expand All @@ -40,6 +47,10 @@
vsts_url state['vsts_url']
vsts_pool state['vsts_pool']
work_folder state['work_folder']
deploymentGroup state['deploymentGroup']
deploymentGroupName state['deploymentGroupName']
projectName state['projectName']
collectionName state['collectionName']

runasservice service_exist?(install_dir)
end
Expand Down Expand Up @@ -97,11 +108,22 @@
unattended: nil,
replace: nil,
url: new_resource.vsts_url,
pool: new_resource.vsts_pool,
agent: new_resource.agent_name,
work: new_resource.work_folder,
}

if new_resource.deploymentGroup
args[:deploymentGroup] = nil
args[:deploymentGroupName] = new_resource.deploymentGroupName || new_resource.vsts_pool
args[:projectName] = new_resource.projectName
args[:collectionName] = new_resource.collectionName if new_resource.collectionName

args[:addDeploymentGroupTags] = nil if new_resource.deploymentGroupTags
args[:deploymentGroupTags] = new_resource.deploymentGroupTags if new_resource.deploymentGroupTags
else
args[:pool] = new_resource.vsts_pool
end

if new_resource.runasservice
args[:runasservice] = nil
args[:windowslogonaccount] = new_resource.windowslogonaccount if windows?
Expand Down Expand Up @@ -149,7 +171,11 @@
group: new_resource.group,
vsts_url: new_resource.vsts_url,
vsts_pool: new_resource.vsts_pool,
work_folder: new_resource.work_folder)
work_folder: new_resource.work_folder,
deploymentGroup: new_resource.deploymentGroup,
deploymentGroupName: new_resource.deploymentGroupName,
projectName: new_resource.projectName,
collectionName: new_resource.collectionName)
Chef::Log.info "'#{new_resource.agent_name}' agent was installed"
end
action :run
Expand Down
2 changes: 2 additions & 0 deletions test/cookbooks/linux-basic/attributes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
default['vsts_agent_test']['vsts_url'] = nil
default['vsts_agent_test']['vsts_pool'] = nil
default['vsts_agent_test']['vsts_token'] = nil
default['vsts_agent_test']['deployment_group_name'] = nil
default['vsts_agent_test']['deployment_group_project'] = nil
default['vsts_agent_test']['username'] = 'vagrant'
27 changes: 27 additions & 0 deletions test/cookbooks/linux-basic/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

agent1_name = "#{node['hostname']}_01"
agent2_name = "#{node['hostname']}_02"
agent3_name = "#{node['hostname']}_deployment_03"

agents_dir = '/opt/agents'

Expand All @@ -37,6 +38,11 @@
action :remove
end

vsts_agent agent3_name do
vsts_token node['vsts_agent_test']['vsts_token']
action :remove
end

log 'Test notification' do
action :nothing
end
Expand Down Expand Up @@ -79,3 +85,24 @@
vsts_agent agent2_name do
action :restart
end

# Agent 3 deployment group
vsts_agent agent3_name do
deploymentGroup true
deploymentGroupName node['vsts_agent_test']['deployment_group_name']
projectName node['vsts_agent_test']['deployment_group_project']
deploymentGroupTags 'web, db'
install_dir "#{agents_dir}/#{agent3_name}"
user 'builder'
group 'builder'
path '/usr/local/bin/:/usr/bin:/opt/bin/:/tmp/'
env('M2_HOME' => '/opt/maven', 'JAVA_HOME' => '/opt/java')
vsts_url node['vsts_agent_test']['vsts_url']
vsts_token node['vsts_agent_test']['vsts_token']
work_folder '/tmp/work'
action :install
end

vsts_agent agent3_name do
action :restart
end
25 changes: 25 additions & 0 deletions test/cookbooks/windows-basic/recipes/default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

agent1_name = "win_#{node['hostname']}_01"
agent2_name = "win_#{node['hostname']}_02"
agent3_name = "win_#{node['hostname']}_deployment_03"

agents_dir = 'C:\\agents'

Expand All @@ -33,6 +34,11 @@
action :remove
end

vsts_agent agent3_name do
vsts_token node['vsts_agent_test']['vsts_token']
action :remove
end

# Agent1
vsts_agent agent1_name do
install_dir "#{agents_dir}\\#{agent1_name}"
Expand Down Expand Up @@ -70,3 +76,22 @@
vsts_agent agent2_name do
action :restart
end

# Agent3
vsts_agent agent3_name do
deploymentGroup true
deploymentGroupName node['vsts_agent_test']['deployment_group_name']
projectName node['vsts_agent_test']['deployment_group_project']
deploymentGroupTags 'web, db'
install_dir "#{agents_dir}\\#{agent2_name}"
user 'builder'
vsts_url node['vsts_agent_test']['vsts_url']
vsts_pool node['vsts_agent_test']['vsts_pool']
vsts_token node['vsts_agent_test']['vsts_token']
windowslogonaccount 'NT AUTHORITY\\NetworkService'
action :install
end

vsts_agent agent3_name do
action :restart
end

0 comments on commit 1fbedb2

Please sign in to comment.