Skip to content

Commit 558b23d

Browse files
authored
winPB: automate jenkins service installation (#2994)
* winPB: automate jenkins service installation * Update JenkinsAgentService.xml.j2 * Update main.yml * Update main.yml * add workspace to be overridden * Update JenkinsAgentService.xml.j2 * add support for pulling jenkins secret from secrets repo * add docs * Update README.md
1 parent dcdcb13 commit 558b23d

File tree

5 files changed

+102
-40
lines changed

5 files changed

+102
-40
lines changed

ansible/playbooks/AdoptOpenJDK_Windows_Playbook/README.md

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,26 @@
22

33
There's a process to setting up Windows machines and getting them connected to Jenkins. If not followed, issues can occur with Jenkins workspaces (See: https://github.com/adoptium/infrastructure/issues/1674).
44

5-
1) Log on to the Windows machine via RDP and run the `ConfigureRemotingForAnsible` commands listed in [main.yml](https://github.com/adoptium/infrastructure/blob/master/ansible/playbooks/AdoptOpenJDK_Windows_Playbook/main.yml).
5+
1. Log on to the Windows machine via RDP and run the `ConfigureRemotingForAnsible` commands listed in [main.yml](https://github.com/adoptium/infrastructure/blob/master/ansible/playbooks/AdoptOpenJDK_Windows_Playbook/main.yml).
66

77
Note: If setting up a win2012r2 machine, `[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12` needs to be executed to stop `Invoke-WebRequest` encountering a `Could not create SSL/TLS secure channel` error. See: https://github.com/adoptium/infrastructure/issues/1858
88

9-
2) Run the playbook on the machine, without skipping the 'adoptopenjdk' and 'jenkins' tags. (See [this](https://github.com/adoptium/infrastructure/blob/master/ansible/README.md) for more information).
9+
1. Run the playbook on the machine, without skipping the 'adoptopenjdk' and 'jenkins' tags. (See [this](https://github.com/adoptium/infrastructure/blob/master/ansible/README.md) for more information).
1010

11-
3) Login as the Jenkins user on the machine via RDP, and ensure access can be gained, and create 2 directories, one for the jenkins workspace ( typically called workspace, and a parallel directory called agent.
11+
1. Login as the Jenkins user on the machine via RDP, and ensure access can be gained, and create 2 directories, one for the jenkins workspace ( typically called workspace, and a parallel directory called agent.
1212

13-
4) On any machine, in a web browser, login to [ci.adoptium.net](https://ci.adoptium.net/), create a new node ( best done as a copy of an existing node ), but ensure the launch option is set to "Launch Agent By Connecting It To A Controller"
13+
1. On any machine, in a web browser, login to [ci.adoptium.net](https://ci.adoptium.net/), create a new node ( best done as a copy of an existing node ), but ensure the launch option is set to "Launch Agent By Connecting It To A Controller"
1414

15-
5) Login as the administrator user on the machine via RDP, and download the relevant [WinSW - Windows Service Wrapper](https://github.com/winsw/winsw/releases/) executable for the platform, e.g [WinSW-x64.exe](https://github.com/winsw/winsw/releases/download/v2.12.0/WinSW-x64.exe) and copy the downloaded file to the agent directory created in step 3.
15+
1. Jenkins service creation is now automated by the [Jenkins_Service_Installation](./roles/Jenkins_Service_Installation/) role which automatically creates the relevant config files and installs [WIN-SW](https://github.com/winsw/winsw). In order to take advantage of this role you must first set a variable called `jenkins_secret` which is set to the secret JNLP string defined in Jenkins when you create the new node. This can be done in one of two ways:
1616

17-
6) In the created agent directory, rename the file downloaded in step 5) from WinSW-x64.exe to something meaningful, e.g. JenkinsAgentService.exe
17+
1. Add the machine to the secrets repo config file in `secrets/vendor_files/Jenkins_Secrets.yml.gpg`. Simply add a new line using the following schema, commit and push:
1818

19-
7) Create an accompanying XML file in the agent directory, and give it the same name as the renamed executable, but with an xml file extension, e.g JenkinsAgentService.xml.
19+
```yaml
20+
<hostname>: <secret>
21+
```
2022
21-
8) Now edit the xml file and populate the file in a similar fashion to the below, the key , edit the fields from the example shown below as appropriate:
22-
- **id** : This should be set to a unique name for the windows service
23-
- **name** : This should be set to a descriptive name, and will be the name of the service displayed on the screen in windows
24-
- **description** : This should be set to a meaningful description
25-
- **executable** : This should be set to the full path to the java executable, that will be used to run the jenkins agent.
26-
- **arguments** This can be obtained from the node configuration page in Jenkins, the *xxxxxx* should reflect the name of the node being created in jenkins, and the *yyyyyyyy* string will be an encoded hex string, used for passing the jenkins user password
27-
- **download from** the URL here should be changed to match the jenkins server name, from which the service can download the agent.jar
23+
2. Set the variable manually in the [adoptopenjdk_variables.yml](./group_vars/all/adoptopenjdk_variables.yml) file.
2824
29-
All other fields can be left as in the example.
25+
Note that the role will be skipped if it cannot find a `jenkins_secret` variable. The role will also not remove any previosuly created service using the previous JNLP process.
3026

31-
> <service>
32-
> <id>Jenkins</id>
33-
> <name>Jenkins</name>
34-
> <description>This service runs an agent for Jenkins automation server.</description>
35-
> <executable>C:\openjdk\jdk-17\bin\java.exe</executable>
36-
> <arguments>-Xrs -jar "%BASE%\agent.jar" -jnlpUrl https://ci.adoptium.net/computer/xxxxxxxxxx/jenkins-agent.jnlp -secret yyyyyyyyyyyyy -workDir=F:\workspace</arguments>
37-
> <logmode>rotate</logmode>
38-
> <onfailure action="restart" />
39-
> <download from="https://ci.adoptium.net/jnlpJars/agent.jar" to="%BASE%\agent.jar"/>
40-
> </service>
41-
42-
9) As the windows administrator, open an elevated command prompt, and now create the Jenkins agent service by following this process :
43-
44-
- cd to the agent directory ( where the executable and xml file are stored )
45-
- run the executable with a parameter install (e.g **.\JenkinsAgentService.exe INSTALL**)
46-
47-
You should get confirmation prompts on screen that the service has been created.
48-
49-
- Next open the windows services dialog, and identify the Jenkins service that has just been created. Right click on the service and select **Properties** from the pop up menu.
50-
- Select the log on tab from the dialog, and change the logon type from local system account to the jenkins account, and enter the password for the jenkins user, followed by **Ok**
51-
- Finally right click on the service, and select **Start** from the pop up menu.
52-
53-
You should now get some confirmations, that the Jenkins user has been granted the log on as a service permission, and you should be able to start the service, and check that the agent is online and available in Jenkins.
54-
5527
The jenkins service should then be started.
56-

ansible/playbooks/AdoptOpenJDK_Windows_Playbook/main.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
- NSClient # Required For Nagios Monitoring
8484
- shortNames
8585
- Dragonwell # Dragonwell bootstrap image
86+
- role: Jenkins_Service_Installation # Automate installing the jenkins service
87+
tags: [jenkins, adoptopenjdk]
88+
when: jenkins_secret is defined # Only run if jenkins_secret is defined
8689
- role: Thunderbird
8790
tags: [Thunderbird, jck, adoptopenjdk]
8891
- role: logs

ansible/playbooks/AdoptOpenJDK_Windows_Playbook/roles/Get_Vendor_Files/tasks/main.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,13 @@
5555
when:
5656
- local_vendor_files.stat.exists
5757
run_once: true
58+
59+
- name: Parse Jenkins_Secrets.yml if it's defined
60+
set_fact: jenkins_secrets="{{ Jenkins_Secrets_yml | from_yaml }}"
61+
when:
62+
- Jenkins_Secrets_yml is defined
63+
64+
- name: Set jenkins_secret for host if defined
65+
set_fact: jenkins_secret="{{ jenkins_secrets[inventory_hostname] }}"
66+
when:
67+
- (jenkins_secrets is defined) and (inventory_hostname in jenkins_secrets)
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
---
2+
###############################
3+
# Jenkins Service Installaion #
4+
###############################
5+
6+
- name: Check if Jenkins service already exists
7+
win_service:
8+
name: Jenkins
9+
register: jenkins_service
10+
failed_when: false
11+
12+
# Allow workspace to be overridden by a variable in inventory.yml
13+
- name: Set workspace variable
14+
set_fact:
15+
workspace: C:\Users\{{ Jenkins_Username }}\workspace
16+
when: (jenkins_service.exists == false) and (workspace is not defined)
17+
18+
- name: Create workspace directory in C:\Users\{{ Jenkins_Username }}
19+
win_file:
20+
path: "{{ workspace }}"
21+
state: directory
22+
recurse: yes
23+
owner: "{{ Jenkins_Username }}"
24+
when: jenkins_service.exists == false
25+
26+
- name: Create agent directory in C:\Users\{{ Jenkins_Username }}
27+
win_file:
28+
path: C:\Users\{{ Jenkins_Username }}\agent
29+
state: directory
30+
recurse: yes
31+
owner: "{{ Jenkins_Username }}"
32+
when: jenkins_service.exists == false
33+
34+
# TODO: Add support for Windows Aarch64 native binary once it's available
35+
- name: Download WIN-SW
36+
win_get_url:
37+
url: https://github.com/winsw/winsw/releases/download/v2.12.0/WinSW-x64.exe
38+
dest: C:\Users\{{ Jenkins_Username }}\agent\JenkinsAgentService.exe
39+
when: jenkins_service.exists == false
40+
41+
- name: Create xml file from template for Jenkins Agent Service
42+
win_template:
43+
src: "{{ role_path }}/templates/JenkinsAgentService.xml.j2"
44+
dest: C:\Users\{{ Jenkins_Username }}\agent\JenkinsAgentService.xml
45+
owner: "{{ Jenkins_Username }}"
46+
when: jenkins_service.exists == false
47+
48+
- name: Install Jenkins Service
49+
win_shell: .\JenkinsAgentService.exe install
50+
args:
51+
chdir: C:\Users\{{ Jenkins_Username }}\agent
52+
when: jenkins_service.exists == false
53+
54+
- name: Grant Jenkins user the Logon as a service right
55+
win_user_right:
56+
name: SeServiceLogonRight
57+
users:
58+
- "{{ Jenkins_Username }}"
59+
action: add
60+
when: jenkins_service.exists == false
61+
62+
- name: Start Jenkins Service
63+
win_service:
64+
name: Jenkins
65+
username: "{{ Jenkins_Username }}"
66+
password: "{{ Jenkins_Win_Passwd }}"
67+
start_mode: auto
68+
state: started
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<service>
2+
<id>Jenkins</id>
3+
<name>Jenkins</name>
4+
<description>This service runs an agent for Jenkins automation server.</description>
5+
<executable>C:\openjdk\jdk-17\bin\java.exe</executable>
6+
<arguments>-Xrs -jar "%BASE%\agent.jar" -jnlpUrl https://ci.adoptium.net/computer/{{ inventory_hostname }}/jenkins-agent.jnlp -secret {{ jenkins_secret }} -workDir=C:\Users\{{ Jenkins_Username }}\workspace</arguments>
7+
<logmode>rotate</logmode>
8+
<onfailure action="restart" />
9+
<download from="https://ci.adoptium.net/jnlpJars/agent.jar" to="%BASE%\agent.jar"/>
10+
</service>

0 commit comments

Comments
 (0)