Commit 96ae404d authored by Bryton Lacquement's avatar Bryton Lacquement 🚪 Committed by Julien Muchembled

recipes: more py3 fixes

parent 77934993
Pipeline #8160 failed with stage
in 0 seconds
...@@ -30,6 +30,7 @@ from six.moves import configparser ...@@ -30,6 +30,7 @@ from six.moves import configparser
import tempfile import tempfile
from slapos.recipe.librecipe import GenericBaseRecipe from slapos.recipe.librecipe import GenericBaseRecipe
from slapos.util import str2bytes
from .certificate_authority import popenCommunicate from .certificate_authority import popenCommunicate
class Recipe(GenericBaseRecipe): class Recipe(GenericBaseRecipe):
...@@ -108,7 +109,7 @@ class Request(Recipe): ...@@ -108,7 +109,7 @@ class Request(Recipe):
request_needed = True request_needed = True
name = self.options['name'] name = self.options['name']
hash_ = hashlib.sha512(name).hexdigest() hash_ = hashlib.sha512(str2bytes(name)).hexdigest()
key = os.path.join(self.ca_private, hash_ + self.ca_key_ext) key = os.path.join(self.ca_private, hash_ + self.ca_key_ext)
certificate = os.path.join(self.ca_certs, hash_ + self.ca_crt_ext) certificate = os.path.join(self.ca_certs, hash_ + self.ca_crt_ext)
......
...@@ -8,7 +8,8 @@ import uuid ...@@ -8,7 +8,8 @@ import uuid
def popenCommunicate(command_list, input=None): def popenCommunicate(command_list, input=None):
subprocess_kw = dict(stdout=subprocess.PIPE, stderr=subprocess.STDOUT) subprocess_kw = dict(stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
universal_newlines=True)
if input is not None: if input is not None:
subprocess_kw.update(stdin=subprocess.PIPE) subprocess_kw.update(stdin=subprocess.PIPE)
popen = subprocess.Popen(command_list, **subprocess_kw) popen = subprocess.Popen(command_list, **subprocess_kw)
......
...@@ -29,6 +29,7 @@ from six.moves import configparser ...@@ -29,6 +29,7 @@ from six.moves import configparser
import os import os
import netaddr import netaddr
import socket import socket
from six.moves import range
class Recipe(object): class Recipe(object):
""" """
...@@ -89,7 +90,7 @@ class Recipe(object): ...@@ -89,7 +90,7 @@ class Recipe(object):
This algorithm thus returns always the same value with the same parameters in This algorithm thus returns always the same value with the same parameters in
a standard environment. a standard environment.
""" """
for port in xrange(self.minimum, self.maximum): for port in range(self.minimum, self.maximum):
sock = socket.socket(self.inet_family, socket.SOCK_STREAM) sock = socket.socket(self.inet_family, socket.SOCK_STREAM)
try: try:
sock.bind((self.ip, port)) sock.bind((self.ip, port))
......
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
import os import os
from hashlib import sha512 from hashlib import sha512
from slapos.recipe.librecipe import GenericBaseRecipe from slapos.recipe.librecipe import GenericBaseRecipe
from slapos.util import str2bytes
class Recipe(GenericBaseRecipe): class Recipe(GenericBaseRecipe):
...@@ -49,7 +50,7 @@ class Callback(GenericBaseRecipe): ...@@ -49,7 +50,7 @@ class Callback(GenericBaseRecipe):
# XXX: hashing the name here and in # XXX: hashing the name here and in
# slapos.toolbox/slapos/pubsub/__init__.py is completely messed up and # slapos.toolbox/slapos/pubsub/__init__.py is completely messed up and
# prevent any debug. # prevent any debug.
callback_id = sha512(notification_id).hexdigest() callback_id = sha512(str2bytes(notification_id)).hexdigest()
filepath = os.path.join(self.options['callbacks'], callback_id) filepath = os.path.join(self.options['callbacks'], callback_id)
self.addLineToFile(filepath, callback) self.addLineToFile(filepath, callback)
......
...@@ -46,7 +46,7 @@ def promise(ssh_client, user, host, port): ...@@ -46,7 +46,7 @@ def promise(ssh_client, user, host, port):
with open(os.devnull) as _dev_null: with open(os.devnull) as _dev_null:
ssh = subprocess.Popen( ssh = subprocess.Popen(
(ssh_client, '%s@%s' % (user, host), '-p', str(port)), (ssh_client, '%s@%s' % (user, host), '-p', str(port)),
stdin=subprocess.PIPE, stdout=_dev_null) stdin=subprocess.PIPE, stdout=_dev_null, universal_newlines=True)
ssh.communicate('q' + chr(255) + chr(0) * 7) ssh.communicate('q' + chr(255) + chr(0) * 7)
if ssh.returncode: if ssh.returncode:
sys.stderr.write("SSH Connection failed\n") sys.stderr.write("SSH Connection failed\n")
......
...@@ -47,7 +47,7 @@ extra_config_dict = %(config)s ...@@ -47,7 +47,7 @@ extra_config_dict = %(config)s
# The solution is to delete all cached 'slapos' modules as well as all cached # The solution is to delete all cached 'slapos' modules as well as all cached
# 'pkg_resources' modules which is responsible of namespace declaration. # 'pkg_resources' modules which is responsible of namespace declaration.
# They will be re-imported again using the updated sys.path # They will be re-imported again using the updated sys.path
for module in sys.modules.keys(): for module in list(sys.modules):
if 'slapos' in module or 'pkg_resources' in module: if 'slapos' in module or 'pkg_resources' in module:
del sys.modules[module] del sys.modules[module]
......
...@@ -38,6 +38,7 @@ import random ...@@ -38,6 +38,7 @@ import random
import string import string
from .librecipe import GenericBaseRecipe from .librecipe import GenericBaseRecipe
from .publish_early import volatileOptions from .publish_early import volatileOptions
from slapos.util import str2bytes
class Integer(object): class Integer(object):
""" """
...@@ -174,7 +175,7 @@ class Password(object): ...@@ -174,7 +175,7 @@ class Password(object):
fd = os.open(self.storage_path, fd = os.open(self.storage_path,
os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_TRUNC, 0o600) os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_TRUNC, 0o600)
try: try:
os.write(fd, self.passwd) os.write(fd, str2bytes(self.passwd))
finally: finally:
os.close(fd) os.close(fd)
if not self.create_once: if not self.create_once:
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
import os import os
import sys import sys
import copy import copy
from ConfigParser import ConfigParser from six.moves.configparser import ConfigParser
import json import json
import subprocess import subprocess
import slapos.slap import slapos.slap
...@@ -38,6 +38,7 @@ import errno ...@@ -38,6 +38,7 @@ import errno
import re import re
import zc.buildout import zc.buildout
import six
class SlapConfigParser(ConfigParser, object): class SlapConfigParser(ConfigParser, object):
""" """
...@@ -57,9 +58,6 @@ class SlapConfigParser(ConfigParser, object): ...@@ -57,9 +58,6 @@ class SlapConfigParser(ConfigParser, object):
def write(self, fp): def write(self, fp):
"""Write an .ini-format representation of the configuration state.""" """Write an .ini-format representation of the configuration state."""
if sys.version_info[0] > 2:
return super(SlapConfigParser, self).write(fp)
regex = re.compile(r'^(.*)\s+([+-]{1})$') regex = re.compile(r'^(.*)\s+([+-]{1})$')
if self._defaults: if self._defaults:
fp.write("[%s]\n" % DEFAULTSECT) fp.write("[%s]\n" % DEFAULTSECT)
...@@ -138,9 +136,9 @@ class Recipe: ...@@ -138,9 +136,9 @@ class Recipe:
for name, ip in self.parameter_dict['ip_list']: for name, ip in self.parameter_dict['ip_list']:
if name: if name:
return name return name
raise AttributeError, "Not network interface found" raise AttributeError("Not network interface found")
def mkdir_p(self, path, mode=0700): def mkdir_p(self, path, mode=0o700):
""" """
Creates a directory and its parents, if needed. Creates a directory and its parents, if needed.
NB: If the directory already exists, it does not change its permission. NB: If the directory already exists, it does not change its permission.
...@@ -193,7 +191,10 @@ class Recipe: ...@@ -193,7 +191,10 @@ class Recipe:
raise zc.buildout.UserError("The specified buildout config file %r does " raise zc.buildout.UserError("The specified buildout config file %r does "
"not exist." % instance_file_path) "not exist." % instance_file_path)
buildout = SlapConfigParser() if six.PY3:
buildout = SlapConfigParser(strict=False)
else:
buildout = SlapConfigParser()
with open(instance_file_path) as instance_path: with open(instance_file_path) as instance_path:
buildout.readfp(instance_path) buildout.readfp(instance_path)
...@@ -231,7 +232,7 @@ class Recipe: ...@@ -231,7 +232,7 @@ class Recipe:
# Copy/paste slap_connection # Copy/paste slap_connection
buildout.add_section('slap-connection') buildout.add_section('slap-connection')
for key, value in self.buildout['slap_connection'].iteritems(): for key, value in six.iteritems(self.buildout['slap_connection']):
# XXX: Waiting for SlapBaseRecipe to use dash instead of underscores # XXX: Waiting for SlapBaseRecipe to use dash instead of underscores
buildout.set('slap-connection', key.replace('_', '-'), value) buildout.set('slap-connection', key.replace('_', '-'), value)
# XXX: Needed for lxc. Use non standard API # XXX: Needed for lxc. Use non standard API
......
...@@ -32,6 +32,7 @@ import re ...@@ -32,6 +32,7 @@ import re
from slapos.recipe.librecipe import GenericBaseRecipe from slapos.recipe.librecipe import GenericBaseRecipe
from slapos.recipe.librecipe.inotify import subfiles from slapos.recipe.librecipe.inotify import subfiles
from slapos.util import str2bytes
# This authority only works with dropbear or openssh sshkey generators # This authority only works with dropbear or openssh sshkey generators
def sshkeys_authority(request_directory, keygen_binary): def sshkeys_authority(request_directory, keygen_binary):
...@@ -112,7 +113,7 @@ class Request(GenericBaseRecipe): ...@@ -112,7 +113,7 @@ class Request(GenericBaseRecipe):
keys_directory = options['keys-directory'] keys_directory = options['keys-directory']
self.private_key = os.path.join(keys_directory, self.private_key = os.path.join(keys_directory,
hashlib.sha256(options['name']).hexdigest()) hashlib.sha256(str2bytes(options['name'])).hexdigest())
self.public_key = self.private_key + '.pub' self.public_key = self.private_key + '.pub'
options['public-key-value'] = '' options['public-key-value'] = ''
......
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
############################################################################## ##############################################################################
import os, subprocess, sys import os, subprocess, sys
import six
class Recipe: class Recipe:
...@@ -41,7 +42,7 @@ class Recipe: ...@@ -41,7 +42,7 @@ class Recipe:
# XXX-Antoine: We gotta find a better way to do this. I tried to check # XXX-Antoine: We gotta find a better way to do this. I tried to check
# out how slapgrid-cp was running buildout. But it is worse than that. # out how slapgrid-cp was running buildout. But it is worse than that.
args = sys.argv[:] args = sys.argv[:]
for x in self.buildout["slap-connection"].iteritems(): for x in six.iteritems(self.buildout["slap-connection"]):
args.append("slap-connection:%s=%s" % x) args.append("slap-connection:%s=%s" % x)
for x in "directory", "eggs-directory", "develop-eggs-directory": for x in "directory", "eggs-directory", "develop-eggs-directory":
args.append("buildout:%s=%s" % (x, self.buildout["buildout"][x])) args.append("buildout:%s=%s" % (x, self.buildout["buildout"][x]))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment