cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
node.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # @cond LICENSE
4 #
5 # Copyright [2010-2013] Institute of Nuclear Physics PAN, Krakow, Poland
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19 # @endcond LICENSE
20 
21 ##
22 # @package src.cm.tools.node
23 # @author Maciej Nabozny <mn@mnabozny.pl>
24 #
25 
26 import subprocess
27 import sys
28 import os
29 
30 from cm.utils.exception import CMException
31 try:
32  sys.path.append('/usr/lib/cc1/')
33  os.environ.setdefault("DJANGO_SETTINGS_MODULE", "cm.settings")
34 
35  from cm.models.node import Node
36  from common.states import node_states
37 except:
38  raise CMException('node_cannot_import_model')
39 
40 
41 # Scripts used to configure distinct systems (especially repositories)
42 debian_script = '''
43 if [ -e /etc/apt/sources.list ] ; then
44  echo 'deb http://cc1.ifj.edu.pl/packages/ wheezy main #CC1' >> /etc/apt/sources.list
45  apt-get update
46  apt-get --yes --force-yes install cc1-node cc1-common cc1-common-networking
47 fi
48 
49 echo "NODE: Creating cc1 user"
50 python /usr/sbin/cc1_setup_user create
51 
52 echo "NODE: Configuring node"
53 python /usr/sbin/cc1_node_configure configure "%(public_key)s"
54 '''
55 
56 redhat_script = '''
57 echo TODO: ala ma kota
58 '''
59 
60 
61 def add(address, username, transport, driver, suffix, cpu, memory, disk):
62  try:
63  Node.objects.get(address=address)
64  raise CMException('node_exists')
65  except:
66  pass
67 
68  node = Node()
69  node.address = address
70  node.comment = ''
71  node.driver = driver
72  node.transport = transport
73  node.username = username
74  node.suffix = suffix
75  node.cpu_total = cpu
76  node.memory_total = memory
77  node.hdd_total = disk
78  node.state = node_states['offline']
79  node.save()
80 
81  return 0
82 
83 
84 def install(node_id, distribution):
85  try:
86  node = Node.objects.get(id=node_id)
87  except:
88  raise CMException('node_not_found')
89 
90  public_key = open('/var/lib/cc1/.ssh/id_rsa.pub').read()
91 
92  # TODO: add password support
93  r = -1
94  if distribution == 'debian':
95  r = subprocess.call(['ssh', '-o', 'PasswordAuthentication=no', 'root@%s' % (node.address),
96  debian_script % {'public_key': public_key}])
97  elif distribution == 'redhat':
98  r = subprocess.call(['ssh', '-o', 'PasswordAuthentication=no', 'root@%s' % (node.address),
99  redhat_script % {'public_key': public_key}])
100  else:
101  raise CMException('node_not_implemented')
102 
103  if r != 0:
104  raise CMException('node_install')
105  else:
106  return 0
107 
108 
109 ##
110 #
111 # interfaces - list of interfaces to communicate with cm and other nodes
112 #
113 def configure(node_id, interfaces):
114  try:
115  node = Node.objects.get(id=node_id)
116  except:
117  raise CMException('node_not_found')
118 
119  try:
120  sys.path.append('/etc/cc1/cm/')
121  import settings
122  except:
123  raise CMException('node_config_invalid')
124 
125  cm_ip = 'echo $SSH_CLIENT | cut -d " " -f 1'
126  r = subprocess.call(['ssh',
127  '-i',
128  '/var/lib/cc1/.ssh/id_rsa',
129  '%s@%s' % (node.username, node.address),
130  'sudo /usr/sbin/cc1_network_setup configure http://`%s`:8003/ %s %s' % (cm_ip, ','.join(interfaces), settings.OSPF_TOKEN)])
131 
132  if r != 0:
133  raise CMException('node_setup_networking')
134 
135  r = subprocess.call(['ssh',
136  '-i',
137  '/var/lib/cc1/.ssh/id_rsa',
138  '%s@%s' % (node.username, node.address),
139  'sudo /usr/sbin/cc1_node_setup_libvirt configure %s %s %s %s %s' %
140  (node.address, node.username, node.transport, node.driver, node.suffix)])
141  if r != 0:
142  raise CMException('node_setup_libvirt')
143 
144 
145 def check(node_id):
146  try:
147  node = Node.objects.get(id=node_id)
148  except:
149  raise CMException('node_not_found')
150 
151  subprocess.call(['ssh', '-i', '/var/lib/cc1/.ssh/id_rsa',
152  '%s@%s' % (node.username, node.address),
153  'sudo /etc/init.d/cc1-node restart'])
154 
155 
156 def start():
157  nodes = Node.objects.all()
158  for node in nodes:
159  if node.state != node_states['locked']:
160  node.state = node_states['offline']
161  node.save()
162 
163  for node in nodes:
164  if node.state != node_states['locked']:
165  subprocess.call(['ssh', '%s@%s' % (node.username, node.address), 'sudo /etc/init.d/cc1-node start'])
166 
167 
168 def node_exec(cmd, state="all"):
169  nodes = []
170  if state == "all":
171  nodes = Node.objects.all()
172  else:
173  nodes = Node.objects.filter(state=node_states[state]).all()
174 
175  for node in nodes:
176  if node.state != node_states['locked']:
177  subprocess.call(['ssh', '%s@%s' % (node.username, node.address), cmd])
178 
179 
180 def remove():
181  print "Not implemented!"
182  return 0
183 
184 
185 def purge():
186  print "Not implemented!"
187  return 0
188