Skip to content
Snippets Groups Projects
Commit fe2ad705 authored by kazix's avatar kazix
Browse files

add scripts to manage users

parent 1c14cb09
No related branches found
No related tags found
No related merge requests found
import gitlab
import json
import argparse
import os
from dotenv import load_dotenv
parser = argparse.ArgumentParser(description="Achtung! Achtung! Delete users form Gitlab")
parser.add_argument("--production", action='store_true', help="Use for Production")
parser.add_argument("--developer", action='store_true', help="Use for Developer")
args = parser.parse_args()
load_dotenv()
try:
if args.production:
token = os.getenv('PRODUCTION_TOKEN')
print('Production Mode, connect to server')
# gl = gitlab.Gitlab(url='https://gitlab.geant.org', private_token=token)
elif args.developer:
token = os.getenv('CE_DEVELOPER_TOKEN')
print('Developer Mode, connect to server')
gl = gitlab.Gitlab(url='https://ribes-89.man.poznan.pl', private_token=token)
else:
print("--production or --developer, please, now exit")
exit()
except:
print('Something wrong or Cant connect to GitLab server, check credential')
exit()
with open('ultimate_users.json', encoding='utf8') as ultimate_users_file:
ultimate_users = json.load(ultimate_users_file)
all_users = ultimate_users['all_users']
# delete users
for user in all_users:
# u = all_users[user]
d = all_users[user]['username']
# new_user = gl.users.create(u)
d_user = gl.users.list(username=d)[0]
d_user.delete()
import gitlab
import requests
import json
import argparse
import os
from dotenv import load_dotenv
import secrets
import string
def randompass():
"""funkcja zwraca losowe hasło"""
# define the alphabet
letters = string.ascii_letters
digits = string.digits
# special_chars = string.punctuation
alphabet = letters + digits
# + special_chars
# fix password length
pwd_length = 16
# generate a password string
pwd = ''
for i in range(pwd_length):
pwd += ''.join(secrets.choice(alphabet))
return pwd
parser = argparse.ArgumentParser(description="Export users and Owners from gitlab")
parser.add_argument("--production", action='store_true', help="Use for Production")
parser.add_argument("--developer", action='store_true', help="Use for Developer")
args = parser.parse_args()
load_dotenv()
try:
if args.production:
token = os.getenv('PRODUCTION_TOKEN')
print('Production Mode, connect to server')
headers = {'PRIVATE-TOKEN': token}
gl = gitlab.Gitlab(url='https://gitlab.geant.org', private_token=token)
gl_proj = 'https://gitlab.geant.org/api/v4/projects'
projects = gl.projects.list(all=True)
elif args.developer:
token = os.getenv('CE_DEVELOPER_TOKEN')
print('Developer Mode, connect to server')
headers = {'PRIVATE-TOKEN': token}
gl = gitlab.Gitlab(url='https://ribes-89.man.poznan.pl', private_token=token)
projects = gl.projects.list()
else:
print("--production or --developer, please, now exit")
exit()
exclude_users = ['root', 'ghost']
ultimate_users = {}
ultimate_users['all_users'] = {}
users = gl.users.list(all=True)
# print(users)
for user in users:
u = gl.users.get(user.id)
if u.username not in exclude_users:
print(u.id)
ultimate_users['all_users'][u.id] = {}
tmp = {
'username': u.username,
'name': u.name,
'state': u.state,
'avatar_url': u.avatar_url,
'web_url': u.web_url,
'bio': u.bio,
'location': u.location,
# 'public_email': u.public_email,
'skype': u.skype,
'linkedin': u.linkedin,
'twitter': u.twitter,
'discord': u.discord,
'website_url': u.website_url,
'organization': u.organization,
'job_title': u.job_title,
'email': u.email,
'commit_email': u.commit_email,
'password': randompass()
}
ultimate_users['all_users'][u.id] = tmp
# print(ultimate_users)
ultimate_users['owners'] = {}
# owners_tmp = {}
for project in projects:
link = (project.attributes['_links']['members'] + '/all')
response = requests.get(link, headers=headers)
data = response.text
owners = json.loads(data)
for owner in owners:
if owner['username'] not in exclude_users:
if owner['access_level'] == 50:
if owner['id'] not in ultimate_users['owners']:
ultimate_users['owners'][owner['id']] = {
'username': owner['username']
}
try:
json_users = json.dumps(ultimate_users, indent=4, ensure_ascii=False)
with open("ultimate_users.json", "w", encoding='utf8') as outfile:
outfile.write(json_users)
print("save json file...")
except:
print('cant save file')
exit()
except:
print('Something wrong or Cant connect to GitLab server, check credential')
exit()
......@@ -52,6 +52,8 @@ try:
check_b_response = requests.get(check_b, headers=headers)
check_b_data = check_b_response.text
check_b_answer = json.loads(check_b_data)
# check approvals
print('Approval Rules:', project.approvalrules.list())
if "default_branch" in check_b_answer:
branch = project.attributes['default_branch']
# x = project.attributes['id']
......
import gitlab
import json
import argparse
import os
from dotenv import load_dotenv
parser = argparse.ArgumentParser(description="Get Project, License and Owners from gitlab")
parser.add_argument("--production", action='store_true', help="Use for Production")
parser.add_argument("--developer", action='store_true', help="Use for Developer")
args = parser.parse_args()
load_dotenv()
try:
if args.production:
token = os.getenv('PRODUCTION_TOKEN')
print('Production Mode, connect to server')
# gl = gitlab.Gitlab(url='https://gitlab.geant.org', private_token=token)
elif args.developer:
token = os.getenv('CE_DEVELOPER_TOKEN')
print('Developer Mode, connect to server')
gl = gitlab.Gitlab(url='https://ribes-89.man.poznan.pl', private_token=token)
else:
print("--production or --developer, please, now exit")
exit()
except:
print('Something wrong or Cant connect to GitLab server, check credential')
exit()
with open('ultimate_users.json', encoding='utf8') as ultimate_users_file:
ultimate_users = json.load(ultimate_users_file)
all_users = ultimate_users['all_users']
# tworzenie usera
for user in all_users:
u = all_users[user]
print("create user: ", u['username'])
new_user = gl.users.create(u)
# zmiana uprawnień
owners = ultimate_users['owners']
for owner in owners:
o = owners[owner]['username']
new_owner = gl.users.list(username=o)[0]
new_owner.can_create_group = 'true'
new_owner.projects_limit = '10'
new_owner.save()
print('add permission for user: ', o)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment