├── tests ├── inventory └── test.yml ├── templates ├── database.local.properties.j2 ├── teamcity-server.j2 ├── teamcity-server.service.j2 ├── teamcity-server.conf.j2 └── database.mysql.properties.j2 ├── handlers └── main.yml ├── tasks ├── main.yml ├── create-teamcity-user.yml ├── license.yml ├── install-plugins.yml ├── mysql-connector.yml └── teamcity-server.yml ├── ansible.cfg ├── meta └── main.yml ├── .travis.yml ├── LICENSE ├── defaults └── main.yml ├── vars └── main.yml └── README.md /tests/inventory: -------------------------------------------------------------------------------- 1 | localhost ansible_connection=local 2 | -------------------------------------------------------------------------------- /templates/database.local.properties.j2: -------------------------------------------------------------------------------- 1 | connectionUrl=jdbc\:hsqldb\:file\:$TEAMCITY_SYSTEM_PATH/buildserver 2 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: restart teamcity-server 3 | service: 4 | name: teamcity-server 5 | state: restarted 6 | -------------------------------------------------------------------------------- /tests/test.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - hosts: localhost 3 | become: yes 4 | roles: 5 | - role: ../roles/ansiblebit.oracle-java 6 | - role: ../roles/ansible-teamcity-server 7 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - include: create-teamcity-user.yml 3 | - include: mysql-connector.yml 4 | when: "teamcity_server_db_type == 'mysql'" 5 | - include: teamcity-server.yml 6 | - include: install-plugins.yml 7 | - include: license.yml 8 | -------------------------------------------------------------------------------- /templates/teamcity-server.j2: -------------------------------------------------------------------------------- 1 | TEAMCITY_SERVER_ENABLED=yes 2 | TEAMCITY_SERVER_PATH="{{ teamcity_server_dir }}" 3 | TEAMCITY_SERVER_MEM_OPTS="-Xms2048m -Xmx2048m -XX:MaxPermSize=1024m" 4 | TEAMCITY_DATA_PATH="{{ teamcity_server_data_dir }}" 5 | TEAMCITY_SERVER_OPTS="" 6 | -------------------------------------------------------------------------------- /ansible.cfg: -------------------------------------------------------------------------------- 1 | [defaults] 2 | timeout = 360 3 | pattern = * 4 | forks = 5 5 | poll_interval = 15 6 | transport = smart 7 | remote_user = ubuntu 8 | roles_path = . 9 | 10 | # disable SSH key host checking 11 | host_key_checking = False 12 | 13 | allow_world_readable_tmpfiles = True 14 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: Mateusz Trojak 4 | description: Installs and automatically configure TeamCity Server. 5 | license: MIT 6 | min_ansible_version: 2.0.0 7 | platforms: 8 | - name: Ubuntu 9 | versions: 10 | - trusty 11 | - xenial 12 | galaxy_tags: 13 | - system 14 | - development 15 | - teamcity 16 | dependencies: 17 | - ansiblebit.oracle-java 18 | -------------------------------------------------------------------------------- /tasks/create-teamcity-user.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: "Create teamcity group" 3 | group: 4 | name: "{{ teamcity_server_group }}" 5 | state: present 6 | system: yes 7 | 8 | - name: "Create teamcity user" 9 | user: 10 | name: "{{ teamcity_server_user }}" 11 | group: "{{ teamcity_server_group }}" 12 | groups: www-data 13 | state: present 14 | shell: /bin/bash 15 | createhome: yes 16 | system: yes 17 | home: "{{ teamcity_server_home }}" 18 | -------------------------------------------------------------------------------- /templates/teamcity-server.service.j2: -------------------------------------------------------------------------------- 1 | #/lib/systemd/system 2 | [Unit] 3 | Description="TeamCity Server" 4 | Requires=network.target 5 | After=syslog.target network.target 6 | 7 | [Service] 8 | Type=forking 9 | User={{ teamcity_server_user }} 10 | Group={{ teamcity_server_user }} 11 | EnvironmentFile=/etc/default/teamcity-server 12 | PIDFile={{ teamcity_server_dir }}/logs/teamcity.pid 13 | ExecStart={{ teamcity_server_dir }}/bin/teamcity-server.sh start 14 | ExecStop={{ teamcity_server_dir }}/bin/teamcity-server.sh stop 15 | 16 | [Install] 17 | WantedBy=multi-user.target 18 | -------------------------------------------------------------------------------- /tasks/license.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: "Check if config folder exists" 3 | file: 4 | path: "{{ teamcity_server_config_dir }}" 5 | state: directory 6 | owner: "{{ teamcity_server_user }}" 7 | group: "{{ teamcity_server_group }}" 8 | mode: 0755 9 | 10 | - name: "Put license keys" 11 | no_log: yes 12 | lineinfile: 13 | dest: "{{ teamcity_server_config_dir }}/license.keys" 14 | line: "{{ item }}" 15 | owner: "{{ teamcity_server_user }}" 16 | group: "{{ teamcity_server_group }}" 17 | create: yes 18 | with_items: "{{ teamcity_server_license_keys }}" 19 | -------------------------------------------------------------------------------- /templates/teamcity-server.conf.j2: -------------------------------------------------------------------------------- 1 | description "TeamCity Server" 2 | 3 | start on runlevel [2345] 4 | stop on runlevel [016] 5 | 6 | # TeamCity can take a while to shutdown 7 | kill timeout 30 8 | 9 | respawn 10 | 11 | pre-start script 12 | 13 | [ ! -f /etc/default/teamcity-server ] && { stop; exit 0; } 14 | 15 | . /etc/default/teamcity-server 16 | 17 | [ "$TEAMCITY_SERVER_ENABLED" != "yes" ] && { stop; exit 0; } 18 | 19 | exit 0 20 | end script 21 | 22 | script 23 | . /etc/default/teamcity-server 24 | 25 | CATALINA_OPTS="$CATALINA_OPTS $TEAMCITY_SERVER_OPTS -server $TEAMCITY_SERVER_MEM_OPTS -Dlog4j.configuration=\"file:${TEAMCITY_SERVER_PATH}/conf/teamcity-server-log4j.xml\" -Dteamcity_logs=${TEAMCITY_SERVER_PATH}/logs -Djava.awt.headless=true" 26 | 27 | export CATALINA_OPTS 28 | 29 | export TEAMCITY_DATA_PATH 30 | 31 | exec start-stop-daemon --start -c {{ teamcity_server_user }} --exec "${TEAMCITY_SERVER_PATH}/bin/catalina.sh" -- run 32 | 33 | end script 34 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: python 3 | python: "2.7" 4 | 5 | matrix: 6 | include: 7 | - os: linux 8 | sudo: required 9 | dist: trusty 10 | - os: linux 11 | sudo: required 12 | dist: xenial 13 | 14 | # Install pip 15 | addons: 16 | apt: 17 | packages: 18 | - python-pip 19 | 20 | before_install: 21 | # Make sure everything's up to date. 22 | - sudo apt-get update -qq 23 | 24 | install: 25 | # Install ansible 26 | - pip2 install ansible 27 | 28 | # Check ansible version 29 | - ansible --version 30 | 31 | # Setup Env 32 | - mkdir -vp roles/ansible-teamcity-server 33 | - ansible-galaxy install --roles-path roles ansiblebit.oracle-java 34 | - mv -v README.md defaults handlers meta tasks templates vars roles/ansible-teamcity-server 35 | - mv ansible.cfg tests 36 | 37 | script: 38 | - cd tests 39 | - ansible-playbook ./test.yml -i inventory --syntax-check 40 | - ansible-playbook -i inventory ./test.yml 41 | 42 | after_script: 43 | - cat /opt/TeamCity/logs/teamcity-server.log 44 | 45 | notifications: 46 | webhooks: https://galaxy.ansible.com/api/v1/notifications/ 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Mateusz Trojak 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /tasks/install-plugins.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: "Check if plugins folder exists" 3 | file: 4 | path: "{{ teamcity_server_plugins_dir }}" 5 | state: directory 6 | owner: "{{ teamcity_server_user }}" 7 | group: "{{ teamcity_server_group }}" 8 | mode: 0755 9 | 10 | - name: "Discover TeamCity plugins enabled on the host" 11 | shell: "ls -1 {{ teamcity_server_plugins_dir }} | sort -u" 12 | register: current_plugins 13 | check_mode: no 14 | changed_when: False 15 | 16 | - name: "Remove plugins that currently are not in use" 17 | file: 18 | path: "{{ teamcity_server_plugins_dir }}/{{ item }}" 19 | state: absent 20 | with_items: "{{ current_plugins.stdout_lines }}" 21 | when: "(item | splitext | first) not in (teamcity_server_plugins | map (attribute='name') | list)" 22 | notify: restart teamcity-server 23 | 24 | - name: "Download TeamCity plugins" 25 | get_url: 26 | url: "{{ item.url }}" 27 | dest: "{{ teamcity_server_plugins_dir }}/{{ item.name }}.zip" 28 | owner: "{{ teamcity_server_user }}" 29 | group: "{{ teamcity_server_group }}" 30 | with_items: "{{ teamcity_server_plugins }}" 31 | notify: restart teamcity-server 32 | -------------------------------------------------------------------------------- /tasks/mysql-connector.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: "Create mysql-connector dir" 3 | file: 4 | path: "{{ teamcity_server_mysql_connector_dir }}" 5 | state: directory 6 | owner: "{{ teamcity_server_user }}" 7 | group: "{{ teamcity_server_group }}" 8 | mode: 0755 9 | 10 | - name: "Create jdbc dir" 11 | file: 12 | path: "{{ teamcity_server_mysql_jdbc_dir }}" 13 | state: directory 14 | owner: "{{ teamcity_server_user }}" 15 | group: "{{ teamcity_server_group }}" 16 | mode: 0755 17 | 18 | - name: "Download and unpack mysql-connector" 19 | unarchive: 20 | src: "http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-{{ teamcity_server_mysql_connector_version }}.tar.gz" 21 | dest: "{{ teamcity_server_mysql_connector_dir }}" 22 | copy: no 23 | 24 | - name: "Create symlink for mysql-connector" 25 | file: 26 | src: "{{ teamcity_server_mysql_connector_dir }}/mysql-connector-java-{{ teamcity_server_mysql_connector_version }}/mysql-connector-java-{{ teamcity_server_mysql_connector_version }}-bin.jar" 27 | dest: "{{ teamcity_server_mysql_jdbc_dir }}/mysql-connector-java-bin.jar" 28 | state: link 29 | owner: "{{ teamcity_server_user }}" 30 | group: "{{ teamcity_server_group }}" 31 | 32 | -------------------------------------------------------------------------------- /defaults/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # Teamcity Version 3 | teamcity_server_version: 2017.2.3 4 | teamcity_server_sha256: 29d163f76a9baf1d630d5275765c72b2a085f7537945d7e7b263b54b1ccece47 5 | 6 | # TeamCity User 7 | teamcity_server_su_user: teamcity 8 | teamcity_server_su_password: teamcity 9 | 10 | # TeamCity Dirs 11 | teamcity_server_install_dir: /opt 12 | teamcity_server_dir: "{{ teamcity_server_install_dir }}/TeamCity" 13 | teamcity_server_data_dir: "{{ teamcity_server_dir }}/BuildServer" 14 | teamcity_server_plugins_dir: "{{ teamcity_server_data_dir }}/plugins" 15 | teamcity_server_config_dir: "{{ teamcity_server_data_dir }}/config" 16 | 17 | # TeamCity Plugins 18 | teamcity_server_plugins: [] 19 | 20 | # Teamcity Licenses 21 | teamcity_server_license_keys: [] 22 | 23 | # MySQL Connector 24 | teamcity_server_mysql_connector_version: 5.1.40 25 | teamcity_server_mysql_connector_dir: "/opt/mysql-connector" 26 | 27 | # MySQL Database User 28 | teamcity_server_mysql_db_user: teamcity 29 | teamcity_server_mysql_db_password: teamcity 30 | teamcity_server_mysql_db_name: teamcity 31 | 32 | # MySQL DB type: Available values: local, mysql 33 | teamcity_server_db_type: "local" 34 | 35 | # MySQL DB Server 36 | teamcity_server_mysql_database_url: localhost 37 | teamcity_server_mysql_database_port: 3306 38 | 39 | # MySQL JDBC Driver Dir 40 | teamcity_server_mysql_jdbc_dir: "{{ teamcity_server_data_dir }}/lib/jdbc" 41 | -------------------------------------------------------------------------------- /templates/database.mysql.properties.j2: -------------------------------------------------------------------------------- 1 | # This is a sample file for configuring TeamCity to use an external database. 2 | # To make it effective, copy it to the "database.properties" file and modify the settings 3 | # according to your environment. 4 | # Do not modify this file, it will be overwritten on the TeamCity server start-up. 5 | # See documentation at https://confluence.jetbrains.com/display/TCD10/Setting+up+an+External+Database 6 | 7 | # Database: MySQL 8 | 9 | connectionUrl=jdbc:mysql://{{ teamcity_server_mysql_database_url }}:{{ teamcity_server_mysql_database_port }}/{{ teamcity_server_mysql_db_name }} 10 | connectionProperties.user={{ teamcity_server_mysql_db_user }} 11 | connectionProperties.password={{ teamcity_server_mysql_db_password }} 12 | 13 | # This option, when set to "true", 14 | # allows reducing memory usage on large query results. 15 | # Should only be used if the jdbc driver version is higher than 5.0.6. 16 | # connectionProperties.useCursorFetch=true 17 | 18 | # The maximum number of connections TeamCity can open on a database server. 19 | maxConnections=50 20 | 21 | # Specifies whether the driver should use the Unicode character encoding 22 | # when handling strings. Should only be used when the driver cannot determine 23 | # the character set mapping, or you are trying to 'force' the 24 | # driver to use Unicode 25 | # 26 | # connectionProperties.useUnicode=true 27 | 28 | # If 'useUnicode' is set to true, the following property determines what 29 | # character encoding the driver should use when dealing with strings 30 | # 31 | # connectionProperties.characterEncoding=UTF-8 32 | 33 | # Specifies whether TeamCity should check each jdbc connection before use. 34 | # It reduces the system performance but avoids cases when the system uses 35 | # a connection that was closed by the database server due to a time-out or this 36 | # connection was broken during the previous operation. 37 | testOnBorrow=true 38 | -------------------------------------------------------------------------------- /vars/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | teamcity_server_admin_user_created: "{{ teamcity_server_dir }}/admin-user-created" 3 | teamcity_server_license_accepted: "{{ teamcity_server_dir }}/license-accepted" 4 | 5 | teamcity_server_bind_port: 8111 6 | 7 | teamcity_server_user: teamcity 8 | teamcity_server_group: teamcity 9 | teamcity_server_home: /home/teamcity 10 | 11 | teamcity_server_plugins: 12 | - name: "crowd-plugin" 13 | url: https://dl.bintray.com/greggigon/generic/teamcity-crowd-plugin-0.3.0.zip 14 | 15 | - name: "ansible-plugin" 16 | url: https://github.com/andreizhuk/tc-ansible-runner/releases/download/v1.0.1/ansible-runner-dist.zip 17 | 18 | - name: "envinject-plugin" 19 | url: https://github.com/kannanekanath/teamcity-envinject-plugin/releases/download/1.0/envinject.zip 20 | 21 | - name: "xunit-plugin" 22 | url: https://github.com/carlpett/xUnit-TeamCity/releases/download/v1.2.0/xUnit.zip 23 | 24 | - name: "github-plugin" 25 | url: http://teamcity.jetbrains.com/guestAuth/repository/download/bt398/lastest.lastSuccessful/teamcity.github.zip 26 | 27 | - name: "commit-hook-plugin" 28 | url: https://teamcity.jetbrains.com/repository/download/TeamCityPluginsByJetBrains_TeamcityCommitHooks_Build/.lastPinned/teamcity-commit-hooks.zip?guest=1 29 | 30 | - name: "docker-plugin" 31 | url: https://teamcity.jetbrains.com/repository/download/TeamCityVirtual_Build/.lastPinned/TeamCity.Virtual.zip?guest=1 32 | 33 | - name: "chuck-plugin" 34 | url: https://dl.bintray.com/dbf/teamcity-chuck-plugin/chucktcplugin-0.5.2.zip 35 | 36 | - name: "jira-plugin" 37 | url: https://bitbucket.org/mamirov/jirareporter/downloads/jirareporter.zip 38 | 39 | - name: "achievements-plugin" 40 | url: http://teamcity.jetbrains.com/repository/download/TeamCityPluginsByJetBrains_TeamcityAchievements_Build/.lastPinned/achievements.zip?guest=1 41 | 42 | - name: "s3cmd-plugin" 43 | url: https://github.com/guardian/teamcity-s3-plugin/releases/download/v1.6.0/s3-plugin.zip 44 | 45 | - name: "build-matrix-plugin" 46 | url: https://github.com/presidentio/teamcity-matrix-build-plugin/releases/download/v1.0.0/matrix-build.zip 47 | 48 | - name: "slack-plugin" 49 | url: https://github.com/PeteGoo/tcSlackBuildNotifier/releases/download/v1.4.6/tcSlackNotificationsPlugin.zip 50 | 51 | - name: "webhook-plugin" 52 | url: https://github.com/tcplugins/tcWebHooks/releases/download/v1.1-alpha8.140.143/tcWebHooksPlugin-1.1-alpha8.140.143.zip 53 | 54 | - name: "sonar-plugin" 55 | url: https://teamcity.jetbrains.com/repository/download/TeamCityPluginsByJetBrains_TeamCitySonarQubePlugin_Build/.lastPinned/sonar-plugin.zip?guest=1 56 | 57 | - name: "buildtag-plugin" 58 | url: https://github.com/echocat/teamcity-buildTagsViaBuildLog-plugin/releases/download/v1.0/BuildTagsViaBuildLog.zip 59 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TeamCity Server 2 | ========= 3 | 4 | [![Build Status](https://travis-ci.org/matisku/ansible-teamcity-server.svg?branch=master)](https://travis-ci.org/matisku/ansible-teamcity-server) 5 | 6 | This role will install and configure TemCity Server - CI tool from JetBrains. 7 | I created this role because I needed to have a fully automated TeamCity setup. 8 | 9 | This role will: 10 | 1. Install TeamCity 11 | 2. Setup database connection (local/mysql) 12 | 3. Setup TeamCity default admin user - `teamcity` 13 | 4. Accept license 14 | 15 | As a result, this role will setup fully working TeamCity Server. 16 | Feel free to use it along with my TeamCity Agent role - [matisku.teamcity-agent](https://github.com/matisku/ansible-teamcity-agent). 17 | 18 | ## Compatibility 19 | This role is compatible with Ubuntu 14.04 and Ubuntu 16.04 20 | 21 | ## Requirements 22 | 1. [ansiblebit.oracle-java](https://github.com/ansiblebit/oracle-java) - Java is required on TeamCity Server 23 | 24 | ## Role Variables 25 | | Variable name | Default value | Description | 26 | |-----------------------------------------|--------------------------------------------------------------------|----------------------------------| 27 | | teamcity_server_version | `2017.2.3` | TeamCity version to install | 28 | | teamcity_server_sha256 | `29d163f76a9baf1d630d5275765c72b2a085f7537945d7e7b263b54b1ccece47` | sha256 for TeamCity package | 29 | | teamcity_server_su_user | `teamcity` | Admin user name for TeamCity | 30 | | teamcity_server_su_password | `teamcity` | Admin user password for TeamCity | 31 | | teamcity_server_install_dir | `/opt` | TeamCity unpack dir | 32 | | teamcity_server_dir | `{{ teamcity_server_install_dir }}/TeamCity` | TeamCity install dir | 33 | | teamcity_server_data_dir | `{{ teamcity_server_dir }}/BuildServer` | TeamCity data/conf/plugins dir | 34 | | teamcity_server_plugins_dir | `{{ teamcity_server_data_dir }}/plugins` | TeamCity plugins dir | 35 | | teamcity_server_license_keys | `[]` | List of TeamCity Licenses | 36 | | teamcity_server_mysql_connector_version | `5.1.40` | MySQL connector version | 37 | | teamcity_server_mysql_connector_dir | `/opt/mysql-connector` | MySQL connector install dir | 38 | | teamcity_server_mysql_db_user | `teamcity` | TeamCity MySQL user name | 39 | | teamcity_server_mysql_db_password | `teamcity` | TeamCity MySQL user password | 40 | | teamcity_server_mysql_db_name | `teamcity` | TeamCity MySQL database | 41 | | teamcity_server_db_type | `local` | Database version: local or mysql | 42 | | teamcity_server_mysql_database_url | `localhost` | MySQL database URL | 43 | | teamcity_server_mysql_database_port | `3306` | MySQL database port | 44 | | teamcity_server_mysql_jdbc_dir | `{{ teamcity_server_data_dir }}/lib/jdbc` | MySQL JDBC driver location | 45 | 46 | ## Dependencies 47 | This role depends on `java` role. 48 | 49 | ## Example Playbook 50 | Example playbook: 51 | 52 | ```yaml 53 | - hosts: teamcity-servers 54 | become: yes 55 | roles: 56 | - matisku.teamcity-server 57 | ``` 58 | 59 | ## Author Information 60 | This role was created by Mateusz Trojak for [Brainly](http://www.brainly.com). 61 | We are using this role for company CI automation with easy failover mechanism. 62 | 63 | ## License 64 | Copyright © 2016-2018 Mateusz Trojak. See LICENSE for details. 65 | -------------------------------------------------------------------------------- /tasks/teamcity-server.yml: -------------------------------------------------------------------------------- 1 | --- 2 | - name: "Get distribution" 3 | get_url: 4 | url: "http://download.jetbrains.com/teamcity/TeamCity-{{ teamcity_server_version }}.tar.gz" 5 | dest: "/tmp/TeamCity-{{ teamcity_server_version }}.tar.gz" 6 | validate_certs: no 7 | timeout: 60 8 | checksum: "sha256:{{ teamcity_server_sha256 }}" 9 | register: _teamcity_package 10 | 11 | - name: "Add TeamCity service default config" 12 | template: 13 | src: "teamcity-server.j2" 14 | dest: "/etc/default/teamcity-server" 15 | mode: 0644 16 | 17 | - name: "Put TeamCity service file (sysv)" 18 | template: 19 | src: "teamcity-server.conf.j2" 20 | dest: "/etc/init/teamcity-server.conf" 21 | mode: 0644 22 | when: "ansible_service_mgr != 'systemd'" 23 | 24 | - name: "Put TeamCity service file (systemd)" 25 | template: 26 | src: "teamcity-server.service.j2" 27 | dest: "/lib/systemd/system/teamcity-server.service" 28 | mode: 0644 29 | when: "ansible_service_mgr == 'systemd'" 30 | 31 | - name: "Stop TeamCity Server before upgrade" 32 | service: 33 | name: teamcity-server 34 | state: stopped 35 | when: _teamcity_package|changed 36 | 37 | - name: "Ensure that upgrade will not fail" 38 | file: 39 | path: "{{ teamcity_server_dir }}/webapps" 40 | state: absent 41 | when: _teamcity_package|changed 42 | 43 | - name: "Unpack distribution" 44 | unarchive: 45 | src: "/tmp/TeamCity-{{ teamcity_server_version }}.tar.gz" 46 | dest: "{{ teamcity_server_install_dir }}" 47 | owner: "{{ teamcity_server_user }}" 48 | group: "{{ teamcity_server_group }}" 49 | copy: "no" 50 | 51 | - name: "Create dir structure" 52 | file: 53 | path: "{{ item }}" 54 | state: directory 55 | owner: "{{ teamcity_server_user }}" 56 | group: "{{ teamcity_server_group }}" 57 | mode: 0755 58 | with_items: 59 | - "{{ teamcity_server_dir }}/logs" 60 | - "{{ teamcity_server_config_dir }}" 61 | 62 | - name: "Add database config" 63 | template: 64 | src: "database.{{ teamcity_server_db_type }}.properties.j2" 65 | dest: "{{ teamcity_server_config_dir }}/database.properties" 66 | owner: "{{ teamcity_server_user }}" 67 | group: "{{ teamcity_server_group }}" 68 | mode: 0644 69 | 70 | - name: "Start TeamCity service" 71 | service: 72 | name: teamcity-server 73 | state: started 74 | enabled: yes 75 | 76 | - name: "Wait until TeamCity starts" 77 | wait_for: 78 | host: 127.0.0.1 79 | port: "{{ teamcity_server_bind_port }}" 80 | delay: 10 81 | timeout: 30 82 | 83 | - name: "Wait for auto-configuration" 84 | uri: 85 | url: "http://127.0.0.1:{{ teamcity_server_bind_port }}/showAgreement.html" 86 | method: GET 87 | status_code: 200 88 | register: _result 89 | until: _result.status == 200 90 | retries: 30 91 | delay: 20 92 | 93 | - name: "Accept License" 94 | uri: 95 | url: "http://127.0.0.1:{{ teamcity_server_bind_port }}/showAgreement.html" 96 | method: POST 97 | headers: 98 | Content-Type: "application/x-www-form-urlencoded" 99 | body: "accept=true" 100 | status_code: 302 101 | creates: "{{ teamcity_server_license_accepted }}" 102 | 103 | - name: "Create license-accepted" 104 | file: 105 | path: "{{ teamcity_server_license_accepted }}" 106 | state: touch 107 | owner: "{{ teamcity_server_user }}" 108 | group: "{{ teamcity_server_group }}" 109 | mode: 0644 110 | 111 | - name: "Get Authentication Token" 112 | shell: "grep -ioE 'Super user authentication token: \"(.*)\"' {{ teamcity_server_dir }}/logs/teamcity-server.log | tail -1 | cut -f2 -d'\"'" 113 | register: _token 114 | check_mode: no 115 | args: 116 | creates: "{{ teamcity_server_admin_user_created }}" 117 | 118 | - name: "Generate base64 for auth" 119 | set_fact: 120 | _base64_token: "{{ (':'~_token.stdout) | b64encode }}" 121 | 122 | - name: "Create Admin User" 123 | uri: 124 | url: "http://127.0.0.1:{{ teamcity_server_bind_port }}/httpAuth/app/rest/users" 125 | method: POST 126 | headers: 127 | Content-Type: "application/json" 128 | Authorization: "Basic {{ _base64_token }}" 129 | body: 130 | username: "{{ teamcity_server_su_user }}" 131 | password: "{{ teamcity_server_su_password }}" 132 | body_format: json 133 | creates: "{{ teamcity_server_admin_user_created }}" 134 | 135 | - name: "Setup Admin User" 136 | uri: 137 | url: "http://127.0.0.1:{{ teamcity_server_bind_port }}/httpAuth/app/rest/users/username:{{ teamcity_server_su_user }}/roles/SYSTEM_ADMIN/g/" 138 | method: PUT 139 | headers: 140 | Content-Type: "application/json" 141 | Authorization: "Basic {{ _base64_token }}" 142 | body_format: json 143 | creates: "{{ teamcity_server_admin_user_created }}" 144 | 145 | - name: "Create admin-user-created" 146 | file: 147 | path: "{{ teamcity_server_admin_user_created }}" 148 | state: touch 149 | owner: "{{ teamcity_server_user }}" 150 | group: "{{ teamcity_server_group }}" 151 | mode: 0644 152 | 153 | - name: "Ensure TeamCity is running" 154 | service: 155 | name: teamcity-server 156 | state: started 157 | enabled: yes 158 | --------------------------------------------------------------------------------