Skip to content

Commit

Permalink
use docker-compose
Browse files Browse the repository at this point in the history
  • Loading branch information
agdula committed Mar 20, 2021
1 parent b548097 commit f8051db
Show file tree
Hide file tree
Showing 37 changed files with 267 additions and 205 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ gitlab-docker/config/ssh*
gitlab-docker/setup/cookies.txt
gitlab-docker/setup/personal-access-token.txt
gitlab-docker/setup/tmp.json
gitlab-docker/setup/project1/git-repo/.git
gitlab-setup/project1/git-repo/.git
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ It also demonstrates a pattern where a "*contract*" is established between appli

Start GitLab and keep it running in one terminal:
```
./build-and-run-gitlab.sh
docker-compose up gitlab-docker
```

GitLab now available at http://localhost/ and you can login with credentials found [here](gitlab-docker/setup/config.txt).
GitLab now available at http://localhost/ and you can login with credentials found [here](gitlab-setup/config.txt).


**2. Setup GitLab**

This script will import some projects into Gitlab and create a personal access token to use in Jenkins.
```
./setup-gitlab.sh
docker-compose up gitlab-setup
```


**3. Start Jenkins**

```
./build-and-run-jenkins.sh
docker-compose up jenkins-docker
```
Jenkins now available on: http://localhost:8080/

Expand Down
7 changes: 0 additions & 7 deletions build-and-run-gitlab.sh

This file was deleted.

8 changes: 0 additions & 8 deletions build-and-run-jenkins.sh

This file was deleted.

4 changes: 0 additions & 4 deletions docker-clean.sh

This file was deleted.

36 changes: 36 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: '3'
services:

gitlab-docker:
build: ./gitlab-docker
ports:
- "443:443"
- "80:80"
- "2222:22"
deploy:
resources:
limits:
memory: 4G
reservations:
memory: 4G
gitlab-setup:
build: ./gitlab-setup

jenkins-docker:
build: ./jenkins-docker
environment:
# todo read this from common volume ../gitlab-docker/setup/personal-access-token.txt
- personal_access_token=$personal_access_token
volumes:
# Share your maven repo and ssh keys to access company resources
- ~/.m2:/var/jenkins_home/.m2
- ~/.ssh:/var/jenkins_home/.ssh
- /var/run/docker.sock:/var/run/docker.sock
ports:
- 8080:8080
deploy:
resources:
limits:
memory: 4G
reservations:
memory: 4G
3 changes: 0 additions & 3 deletions docker-stop.sh

This file was deleted.

2 changes: 2 additions & 0 deletions gitlab-docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM gitlab/gitlab-ce:latest
COPY config/gitlab.rb /etc/gitlab/gitlab.rb
2 changes: 0 additions & 2 deletions gitlab-docker/bash-gitlab.sh

This file was deleted.

3 changes: 0 additions & 3 deletions gitlab-docker/setup/config.txt

This file was deleted.

41 changes: 0 additions & 41 deletions gitlab-docker/setup/create-personal-access-token.sh

This file was deleted.

17 changes: 0 additions & 17 deletions gitlab-docker/start-gitlab.sh

This file was deleted.

4 changes: 0 additions & 4 deletions gitlab-docker/stop-gitlab.sh

This file was deleted.

24 changes: 24 additions & 0 deletions gitlab-setup/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM ubuntu:20.04

RUN apt-get update && \
apt-get install -y software-properties-common && \
add-apt-repository -y ppa:deadsnakes/ppa && \
apt-mark hold python2 python2-minimal python2.7 python2.7-minimal libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib && \
apt-get install -y \
python3.9 \
python3-pip \
python-is-python3 \
curl \
gettext-base \
git \
&& rm -rf /var/lib/apt/lists/*

RUN pip3 install requests beautifulsoup4 lxml

RUN git config --global user.email "noone@nowhere.com" && \
git config --global user.name "Provisioning Script"

COPY . /bootstrap

CMD ["/bootstrap/main.sh"]

2 changes: 2 additions & 0 deletions gitlab-docker/setup/add-ssh-key.sh → gitlab-setup/add-ssh-key.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -eux

source config.txt
export $(cut -d= -f1 config.txt)
token=$(cat personal-access-token.txt)
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions gitlab-setup/config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gitlab_host_name=gitlab-docker
gitlab_host=http://$gitlab_host_name
gitlab_user="root"
gitlab_password="thepassword" #From gitlab.rb
4 changes: 3 additions & 1 deletion gitlab-docker/setup/create-projects.sh → gitlab-setup/create-projects.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -eux

source config.txt
export $(cut -d= -f1 config.txt)
token=$(cat personal-access-token.txt)
Expand All @@ -18,7 +20,7 @@ for i in `seq 1 20`; do
git init
git add .
git commit -a -m first
git remote add origin http://$gitlab_user:$gitlab_password@localhost/root/${projectName}.git
git remote add origin http://$gitlab_user:$gitlab_password@$gitlab_host_name/root/${projectName}.git
git push -u origin master
cd -
done
File renamed without changes.
2 changes: 2 additions & 0 deletions gitlab-docker/setup/global-settings.sh → gitlab-setup/global-settings.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -eux

source config.txt
export $(cut -d= -f1 config.txt)
token=$(cat personal-access-token.txt)
Expand Down
14 changes: 14 additions & 0 deletions gitlab-setup/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -eux

cd "$(dirname "$0")"

source config.txt
export $(cut -d= -f1 config.txt)

./personal_access_token.py > personal-access-token.txt
./global-settings.sh
./add-ssh-key.sh
./create-projects.sh

89 changes: 89 additions & 0 deletions gitlab-setup/personal_access_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/python3
"""
https://github.com/vitalyisaev2/gitlab_token
Script that creates Personal Access Token for Gitlab API;
Tested with:
- Gitlab Community Edition 10.1.4
- Gitlab Enterprise Edition 12.6.2
- Gitlab Enterprise Edition 13.4.4
"""
import os
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin

endpoint = os.environ.get('gitlab_host') or 'http://gitlab-docker'
root_route = urljoin(endpoint, "/")
sign_in_route = urljoin(endpoint, "/users/sign_in")
pat_route = urljoin(endpoint, "/-/profile/personal_access_tokens")

login = os.environ.get('gitlab_user') or 'root'
password = os.environ.get('gitlab_password') or 'thepassword'


def find_csrf_token(text):
soup = BeautifulSoup(text, "lxml")
token = soup.find(attrs={"name": "csrf-token"})
param = soup.find(attrs={"name": "csrf-param"})
data = {param.get("content"): token.get("content")}
return data


def obtain_csrf_token():
r = requests.get(root_route)
token = find_csrf_token(r.text)
return token, r.cookies


def obtain_authenticity_token(cookies):
r = requests.get(pat_route, cookies=cookies)
soup = BeautifulSoup(r.text, "lxml")
token = soup.find('input', attrs={'name': 'authenticity_token', 'type': 'hidden'}).get('value')
return token


def sign_in(csrf, cookies):
data = {
"user[login]": login,
"user[password]": password,
"user[remember_me]": 0,
"utf8": "✓"
}
data.update(csrf)
r = requests.post(sign_in_route, data=data, cookies=cookies)
token = find_csrf_token(r.text)
return token, r.history[0].cookies


def obtain_personal_access_token(name, expires_at, csrf, cookies, authenticity_token):
data = {
"personal_access_token[expires_at]": expires_at,
"personal_access_token[name]": name,
"personal_access_token[scopes][]": "api",
"authenticity_token": authenticity_token,
"utf8": "✓"
}
data.update(csrf)
r = requests.post(pat_route, data=data, cookies=cookies)
soup = BeautifulSoup(r.text, "lxml")
token = soup.find('input', id='created-personal-access-token').get('value')
return token


def main():
csrf1, cookies1 = obtain_csrf_token()
# print("root", csrf1, cookies1)
csrf2, cookies2 = sign_in(csrf1, cookies1)
# print("sign_in", csrf2, cookies2)
authenticity_token = obtain_authenticity_token(cookies2)

name = "Provisioner"
expires_at = ""
token = obtain_personal_access_token(name, expires_at, csrf2, cookies2, authenticity_token)
print(token)


if __name__ == "__main__":
main()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit f8051db

Please sign in to comment.