Skip to content
Snippets Groups Projects
Commit b3238a7b authored by Trung Nguyen's avatar Trung Nguyen
Browse files

Merge branch 'cloudinit' into 'master'

cloudinit

See merge request !377
parents 5f04692b 2613da8b
No related branches found
No related tags found
1 merge request!377cloudinit
#!/bin/python -E
from jinja2 import Template, Environment, FileSystemLoader
import itertools
import subprocess
import datetime
import os
import sys
import time
import socket
from subprocess import call
import re
import json
def grab_card_ids():
# This method runs nvidia-smi to grab the card ids, then returns a list
if not os.path.isfile("/bin/nvidia-smi"):
print("nvidia-smi binary not found!")
exit(1)
cmd = ["/bin/nvidia-smi", "--query-gpu=pci.bus_id","--format=csv,noheader"]
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
cards = []
for line in p.stdout.readlines():
line = line.rstrip().split(":")[2]
pcibus_num = int(re.sub('[.:]', '', line).rstrip("0"),16)
card = "PCI:0:{}:0".format(str(pcibus_num))
cards.append(card)
return cards
def grab_card_boardname():
if not os.path.isfile("/bin/nvidia-smi"):
print("nvidia-smi binary not found!")
exit(1)
cmd = ["/bin/nvidia-smi", "--query-gpu=name","--format=csv,noheader"]
cards = []
p = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in p.stdout.readlines():
line = line.rstrip()
cards.append(line)
return cards
def write_xorg_conf(cards):
num_of_cards = len(cards) + 1
boardname = (grab_card_boardname())[0]
gpus = []
file_loader = FileSystemLoader('/opt/generate-xorg/template')
env = Environment(loader=file_loader)
template = env.get_template('xorg.conf.j2')
for i in range(1, num_of_cards):
monitors = []
screens = []
res = list(itertools.combinations(cards,i))
for j in range(i):
monitors.append("Monitor"+str(j))
screens.append("Screen"+str(j))
for card in res:
filename = "/etc/X11/xorg.conf." + str(i) + str(res.index(card))
template.stream({'boardname':boardname,'monitors':monitors,'screens':screens,'devices':card}).dump(filename)
if __name__ == "__main__":
cards = grab_card_ids()
write_xorg_conf(cards)
# nvidia-xconfig: X configuration file generated by nvidia-xconfig
# nvidia-xconfig: version 375.66 (buildmeister@swio-display-x86-rhel47-06) Mon May 1 15:45:32 PDT 2017
Section "DRI"
Mode 0666
EndSection
Section "ServerLayout"
#InputDevice "Keyboard0" "CoreKeyboard"
#InputDevice "Mouse0" "CorePointer"
Identifier "Layout0"
{% for screen in screens %}
{% if screens.index(screen) == 0 %}
Screen 0 "Screen{{screens.index(screen)}}"
{% else %}
Screen {{screens.index(screen)}} "Screen{{screens.index(screen)}}" RightOf "Screen{{screens.index(screen)-1}}"
{% endif %}
{% endfor %}
#InputDevice "Keyboard0" "CoreKeyboard"
#InputDevice "Mouse0" "CorePointer"
EndSection
Section "Files"
FontPath "/usr/share/fonts/default/Type1"
EndSection
Section "InputDevice"
# generated from default
Identifier "Mouse0"
Driver "mouse"
Option "Protocol" "auto"
Option "Device" "/dev/input/mice"
Option "Emulate3Buttons" "no"
Option "ZAxisMapping" "4 5"
EndSection
Section "InputDevice"
# generated from default
Identifier "Keyboard0"
Driver "kbd"
EndSection
{% for monitor in monitors %}
Section "Monitor"
Identifier "{{monitor}}"
VendorName "Unknown"
ModelName "Unknown"
HorizSync 28.0 - 33.0
VertRefresh 43.0 - 72.0
Option "DPMS"
EndSection
{% endfor %}
{% for device in devices %}
Section "Device"
Identifier "Device{{devices.index(device)}}"
Driver "nvidia"
VendorName "NVIDIA Corporation"
boardname "{{boardname}}"
BusID "{{device}}"
EndSection
{% endfor %}
{% for screen in screens %}
Section "Screen"
Identifier "Screen{{screens.index(screen)}}"
Device "Device{{screens.index(screen)}}"
Monitor "Monitor{{screens.index(screen)}}"
DefaultDepth 24
Option "ProbeAllGpus" "false"
{% if boardname == 'GRID K1' %}
Option "UseDisplayDevice" "None"
{% endif %}
SubSection "Display"
Virtual 1920 1200
Depth 24
EndSubSection
EndSection
{% endfor -%}
---
- name: create /opt/generate-xorg and template dirs
become: yes
file:
path: /opt/generate-xorg/template
state: directory
mode: '0755'
- name: copy nvidia-xconf-gen.py
become: yes
copy:
src: nvidia-xconf-gen.py
dest: /opt/generate-xorg/nvidia-xconf-gen.py
owner: root
mode: '0755'
- name: copy xorg.conf.j2 template
become: yes
copy:
src: xorg.conf.j2
dest: /opt/generate-xorg/template/xorg.conf.j2
owner: root
mode: '0644'
- name: Creates ansible-generate-xorg file under /etc/cron.d
become: yes
cron:
name: cron job to generate xorg after reboot
special_time: reboot
user: root
job: "/opt/generate-xorg/nvidia-xconf-gen.py"
cron_file: ansible-generate-xorg
#!/bin/bash
hpcca_public_key='cert-authority ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfHlWGrnpirvqvUTySnoQK6ze5oIXz7cYIT+XCBeBCahlK05O38g0erBGrNWFozZwbIXnysVCibaUJqtH0JrYqmcr2NnYA0PoiTeranvaJI7pQsga1gBxfK/D4UItw5yI6V7w9efMT0zpIP8WEubQz6GFtkyiNVgFCHj3+VhLs3RslvYzb35SFcLXEDsGVQM5NdWBUgRaNRqpTPvuMcxTyPvy32wW72kwaYRQioDJFcE2WJ240M2oSsx+dhTWvI8sW1sEUI1qIDfyBPsOgsLofuSpt4ZNgJqBUTp/hW85wVpNzud6A4YJWHpZXSDMtUMYE9QL+x2fw/b26yck9ZPE/ hines@tun'
mkdir -p /home/ec2-user/.ssh
echo $hpcca_public_key >> /home/ec2-user/.ssh/authorized_keys
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment