Commit 40fb8650 authored by Bryton Lacquement's avatar Bryton Lacquement 🚪

recipe: add more Python 3 support

parent b686950d
......@@ -30,6 +30,7 @@ from six.moves import configparser
import tempfile
from slapos.recipe.librecipe import GenericBaseRecipe
from slapos.util import str2bytes
from .certificate_authority import popenCommunicate
class Recipe(GenericBaseRecipe):
......@@ -108,7 +109,7 @@ class Request(Recipe):
request_needed = True
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)
certificate = os.path.join(self.ca_certs, hash_ + self.ca_crt_ext)
......
......@@ -8,7 +8,8 @@ import uuid
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:
subprocess_kw.update(stdin=subprocess.PIPE)
popen = subprocess.Popen(command_list, **subprocess_kw)
......
......@@ -29,6 +29,7 @@ from six.moves import configparser
import os
import netaddr
import socket
from six.moves import range
class Recipe(object):
"""
......@@ -89,7 +90,7 @@ class Recipe(object):
This algorithm thus returns always the same value with the same parameters in
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)
try:
sock.bind((self.ip, port))
......
......@@ -27,6 +27,7 @@
import os
from hashlib import sha512
from slapos.recipe.librecipe import GenericBaseRecipe
from slapos.util import str2bytes
class Recipe(GenericBaseRecipe):
......@@ -49,7 +50,7 @@ class Callback(GenericBaseRecipe):
# XXX: hashing the name here and in
# slapos.toolbox/slapos/pubsub/__init__.py is completely messed up and
# 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)
self.addLineToFile(filepath, callback)
......
......@@ -46,7 +46,7 @@ def promise(ssh_client, user, host, port):
with open(os.devnull) as _dev_null:
ssh = subprocess.Popen(
(ssh_client, '%s@%s' % (user, host), '-p', str(port)),
stdin=subprocess.PIPE, stdout=_dev_null)
stdin=subprocess.PIPE, stdout=_dev_null, universal_newlines=True)
  • The diff is fine but I see something wrong: passing a read-only fd to stdout does not work.

    >>> with open('/dev/null') as f: subprocess.check_call(('echo', 'foo'), stdout=f)
    ... 
    echo: write error: Bad file descriptor

    /cc @Nicolas

    Edited by Julien Muchembled
  • Hi @jm ,

    I only see your message now when browsing my gitlab notifications, as for some reason I didn't get notified by mail of this mention...

    I have pushed a fix : 8c83f2e7

Please register or sign in to reply
ssh.communicate('q' + chr(255) + chr(0) * 7)
if ssh.returncode:
sys.stderr.write("SSH Connection failed\n")
......
......@@ -38,6 +38,7 @@ import random
import string
from .librecipe import GenericBaseRecipe
from .publish_early import volatileOptions
from slapos.util import str2bytes
class Integer(object):
"""
......@@ -174,7 +175,7 @@ class Password(object):
fd = os.open(self.storage_path,
os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_TRUNC, 0o600)
try:
os.write(fd, self.passwd)
os.write(fd, str2bytes(self.passwd))
finally:
os.close(fd)
if not self.create_once:
......
......@@ -28,7 +28,7 @@
import os
import sys
import copy
from ConfigParser import ConfigParser
from six.moves.configparser import ConfigParser
import json
import subprocess
import slapos.slap
......@@ -38,6 +38,7 @@ import errno
import re
import zc.buildout
import six
class SlapConfigParser(ConfigParser, object):
"""
......@@ -135,9 +136,9 @@ class Recipe:
for name, ip in self.parameter_dict['ip_list']:
if 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.
NB: If the directory already exists, it does not change its permission.
......@@ -190,6 +191,9 @@ class Recipe:
raise zc.buildout.UserError("The specified buildout config file %r does "
"not exist." % instance_file_path)
try:
buildout = SlapConfigParser(strict=False)
except TypeError:
buildout = SlapConfigParser()
with open(instance_file_path) as instance_path:
buildout.readfp(instance_path)
......@@ -228,7 +232,7 @@ class Recipe:
# Copy/paste 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
buildout.set('slap-connection', key.replace('_', '-'), value)
# XXX: Needed for lxc. Use non standard API
......
......@@ -32,6 +32,7 @@ import re
from slapos.recipe.librecipe import GenericBaseRecipe
from slapos.recipe.librecipe.inotify import subfiles
from slapos.util import str2bytes
# This authority only works with dropbear or openssh sshkey generators
def sshkeys_authority(request_directory, keygen_binary):
......@@ -112,7 +113,7 @@ class Request(GenericBaseRecipe):
keys_directory = options['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'
options['public-key-value'] = ''
......
......@@ -26,6 +26,7 @@
##############################################################################
import os, subprocess, sys
import six
class Recipe:
......@@ -41,7 +42,7 @@ class Recipe:
# 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.
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)
for x in "directory", "eggs-directory", "develop-eggs-directory":
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