[Freeipa-devel] [PATCH] 0008 Modify /etc/sysconfig/network on a client when IPA manages hostname

2011-07-29 Thread Alexander Bokovoy

https://fedorahosted.org/freeipa/ticket/1368

also replaces a tab by spaces in one else statement (cosmetic).
-- 
/ Alexander Bokovoy
From bc02d3098671a2284b5764205b893facdeacf80e Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy aboko...@redhat.com
Date: Tue, 19 Jul 2011 15:33:53 +0300
Subject: [PATCH] Modify /etc/sysconfig/network on a client when IPA manages
 hostname

https://fedorahosted.org/freeipa/ticket/1368
---
 ipa-client/ipa-install/ipa-client-install |   35 ++--
 1 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/ipa-client/ipa-install/ipa-client-install 
b/ipa-client/ipa-install/ipa-client-install
index 
2e1a28ca087dee9eea04ccc7a9e6e4f8ce89..0199dc69049a484d92fcace3721de0cd04dbe1ad
 100755
--- a/ipa-client/ipa-install/ipa-client-install
+++ b/ipa-client/ipa-install/ipa-client-install
@@ -28,10 +28,11 @@ try:
 import logging
 import tempfile
 import getpass
+import re
 from ipaclient import ipadiscovery
 import ipaclient.ipachangeconf
 import ipaclient.ntpconf
-from ipapython.ipautil import run, user_input, CalledProcessError, 
file_exists
+from ipapython.ipautil import run, user_input, CalledProcessError, 
file_exists, install_file
 from ipapython import ipautil
 from ipapython import dnsclient
 from ipapython import sysrestore
@@ -520,6 +521,30 @@ def configure_certmonger(fstore, subject_base, cli_realm, 
hostname, options):
 except:
 print certmonger request for host certificate failed
 
+def backup_and_replace_hostname(fstore, hostname):
+# TODO: this code is for Red Hat-based systems
+#   it need to be rewritten for cross-paltform support
+#   so that different configuration backends would be possible
+#   (GNU/Debian stores this information in a different place)
+network_filename = /etc/sysconfig/network
+# Backup original /etc/sysconfig/network
+fstore.backup_file(network_filename)
+hostname_pattern = re.compile('^HOSTNAME=')
+temp_filename = None
+with tempfile.NamedTemporaryFile(delete=False) as new_config:
+temp_filename = new_config.name
+with open(network_filename, 'r') as f:
+for line in f:
+if hostname_pattern.match(line):
+new_config.write(HOSTNAME=%s\n % (hostname))
+else:
+new_config.write(line)
+new_config.flush()
+# At this point new_config is closed but not removed due to 'delete=False' 
above
+# Now, install the temporary file as configuration and ensure old version 
is available as .orig
+# While .orig file is not used during uninstall, it is left there for 
administrator.
+install_file(temp_filename, network_filename)
+
 def configure_sssd_conf(fstore, cli_realm, cli_domain, cli_server, options):
 sssdconfig = SSSDConfig.SSSDConfig()
 sssdconfig.new_config()
@@ -566,6 +591,10 @@ def configure_sssd_conf(fstore, cli_realm, cli_domain, 
cli_server, options):
 sssdconfig.save_domain(domain)
 sssdconfig.write(/etc/sssd/sssd.conf)
 
+# configure /etc/sysconfig/network to contain the hostname we set.
+if options.hostname:
+backup_and_replace_hostname(fstore, options.hostname)
+
 return 0
 
 def resolve_ipaddress(server):
@@ -940,8 +969,8 @@ def main():
 if not options.sssd:
 print sys.stderr, Failed to configure automatic startup of 
the NSCD daemon
 print sys.stderr, Caching of users/groups will not be 
available after reboot
-   else:
-   print sys.stderr, Failed to disable NSCD daemon. Please 
disable it manually.
+else:
+print sys.stderr, Failed to disable NSCD daemon. Please 
disable it manually.
 
 else:
 # this is optional service, just log
-- 
1.7.6

___
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel

Re: [Freeipa-devel] [PATCH] 0008 Modify /etc/sysconfig/network on a client when IPA manages hostname

2011-07-29 Thread Martin Kosek
On Fri, 2011-07-29 at 11:42 +0300, Alexander Bokovoy wrote:
 https://fedorahosted.org/freeipa/ticket/1368
 
 also replaces a tab by spaces in one else statement (cosmetic).

This works fine. But I have few suggestion for improvement:

1) Shouldn't we also run `hostname NEW_HOSTNAME` so that the new
hostname is properly set on the system?

2) I would enhance our man pages/help and state that we are changing the
system hostname. Current --hostname option is confusing:

   --hostname
  The hostname of this server (FQDN).  By  default  of  nodename  
from
  uname(2) is used.

Martin

___
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel


Re: [Freeipa-devel] [PATCH] 0008 Modify /etc/sysconfig/network on a client when IPA manages hostname

2011-07-29 Thread Alexander Bokovoy
On 29.07.2011 12:01, Martin Kosek wrote:
 On Fri, 2011-07-29 at 11:42 +0300, Alexander Bokovoy wrote:
 https://fedorahosted.org/freeipa/ticket/1368

 also replaces a tab by spaces in one else statement (cosmetic).
 
 This works fine. But I have few suggestion for improvement:
 
 1) Shouldn't we also run `hostname NEW_HOSTNAME` so that the new
 hostname is properly set on the system?
Makes sense.

 2) I would enhance our man pages/help and state that we are changing the
 system hostname. Current --hostname option is confusing:
 
--hostname
   The hostname of this server (FQDN).  By  default  of  nodename  
 from
   uname(2) is used.
Oh, this is not informative at all. I'll get this updated.

Thanks for review!
-- 
/ Alexander Bokovoy

___
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel


Re: [Freeipa-devel] [PATCH] 0008 Modify /etc/sysconfig/network on a client when IPA manages hostname

2011-07-29 Thread Martin Kosek
On Fri, 2011-07-29 at 12:46 +0300, Alexander Bokovoy wrote:
 On 29.07.2011 12:21, Alexander Bokovoy wrote:
  On 29.07.2011 12:01, Martin Kosek wrote:
  On Fri, 2011-07-29 at 11:42 +0300, Alexander Bokovoy wrote:
  https://fedorahosted.org/freeipa/ticket/1368
 
  also replaces a tab by spaces in one else statement (cosmetic).
 
  This works fine. But I have few suggestion for improvement:
 
  1) Shouldn't we also run `hostname NEW_HOSTNAME` so that the new
  hostname is properly set on the system?
  Makes sense.
  
  2) I would enhance our man pages/help and state that we are changing the
  system hostname. Current --hostname option is confusing:
 
 --hostname
The hostname of this server (FQDN).  By  default  of  
  nodename  from
uname(2) is used.
  Oh, this is not informative at all. I'll get this updated.
 Updated patch attached.
 

Ok, hostname is properly changed now. I still have some issues:

1) Updated --hostname help doc line in the source code is too long. This
should be split.

2) I miss new --hostname help in ipa-client-install man pages (there can
be the same text as it is in the inline help)

3) When IPA client is uninstalled, I would consider changing the
hostname back to where it was. sysrestore.StateFile could be used for
storing the old hostname value.

Martin

___
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel


Re: [Freeipa-devel] [PATCH] 0008 Modify /etc/sysconfig/network on a client when IPA manages hostname

2011-07-29 Thread Alexander Bokovoy
On 29.07.2011 13:52, Martin Kosek wrote:
 Oh, this is not informative at all. I'll get this updated.
 Updated patch attached.
 Ok, hostname is properly changed now. I still have some issues:
 
 1) Updated --hostname help doc line in the source code is too long. This
 should be split.
Now it uses multiple lines.

 2) I miss new --hostname help in ipa-client-install man pages (there can
 be the same text as it is in the inline help)
Copied the same text to ipa-client-install.1

 3) When IPA client is uninstalled, I would consider changing the
 hostname back to where it was. sysrestore.StateFile could be used for
 storing the old hostname value.
Added use of sysrestore.StateFile and restoring the hostname from it.
Note that /etc/sysconfig/network is restored already via
sysrestore.FileStore.

-- 
/ Alexander Bokovoy
From c1892612c7ad64f8ea9ae14f8077d0a5a4b832bf Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy aboko...@redhat.com
Date: Tue, 19 Jul 2011 15:33:53 +0300
Subject: [PATCH] Modify /etc/sysconfig/network on a client when IPA manages
 hostname

https://fedorahosted.org/freeipa/ticket/1368
---
 ipa-client/ipa-install/ipa-client-install |   54 ++--
 ipa-client/man/ipa-client-install.1   |2 +-
 2 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/ipa-client/ipa-install/ipa-client-install 
b/ipa-client/ipa-install/ipa-client-install
index 
2e1a28ca087dee9eea04ccc7a9e6e4f8ce89..509e67bd02394b1ca62ab6ec0d5bf313cba646dd
 100755
--- a/ipa-client/ipa-install/ipa-client-install
+++ b/ipa-client/ipa-install/ipa-client-install
@@ -28,10 +28,11 @@ try:
 import logging
 import tempfile
 import getpass
+import re
 from ipaclient import ipadiscovery
 import ipaclient.ipachangeconf
 import ipaclient.ntpconf
-from ipapython.ipautil import run, user_input, CalledProcessError, 
file_exists
+from ipapython.ipautil import run, user_input, CalledProcessError, 
file_exists, install_file
 from ipapython import ipautil
 from ipapython import dnsclient
 from ipapython import sysrestore
@@ -87,7 +88,9 @@ def parse_options():
 parser.add_option(, --uninstall, dest=uninstall, action=store_true,
   default=False, help=uninstall an existing installation)
 parser.add_option(, --hostname, dest=hostname,
-  help=The hostname of this server (FQDN). By default of 
nodename from uname(2) is used.)
+  help=The hostname of this server (FQDN). If specified, 
the hostname will be set and 
+   the system configuration will be updated to 
persist over reboot. 
+   By default a nodename result from uname(2) is 
used.)
 parser.add_option(, --enable-dns-updates, dest=dns_updates, 
action=store_true, default=False,
   help=Configures the machine to attempt dns updates when 
the ip address changes.)
 parser.add_option(--no-krb5-offline-passwords, 
dest=krb5_offline_passwords, action=store_false,
@@ -236,6 +239,12 @@ def uninstall(options, env):
 
 print Restoring client configuration files
 fstore.restore_all_files()
+old_hostname = statestore.restore_state('network','hostname')
+if not hostname is None and old_hostname != hostname:
+try:
+ipautil.run(['/bin/hostname', old_hostname])
+except CalledProcessError, e:
+print sys.stderr, Failed to set this machine hostname to %s 
(%s). % (old_hostname, str(e))
 
 if ipautil.service_is_installed('nscd'):
 try:
@@ -520,6 +529,36 @@ def configure_certmonger(fstore, subject_base, cli_realm, 
hostname, options):
 except:
 print certmonger request for host certificate failed
 
+def backup_and_replace_hostname(fstore, hostname):
+# TODO: this code is for Red Hat-based systems
+#   it need to be rewritten for cross-paltform support
+#   so that different configuration backends would be possible
+#   (GNU/Debian stores this information in a different place)
+network_filename = /etc/sysconfig/network
+# Backup original /etc/sysconfig/network
+fstore.backup_file(network_filename)
+hostname_pattern = re.compile('^HOSTNAME=(.*)')
+temp_filename = None
+with tempfile.NamedTemporaryFile(delete=False) as new_config:
+temp_filename = new_config.name
+with open(network_filename, 'r') as f:
+for line in f:
+m = hostname_pattern.match(line)
+if m:
+new_config.write(HOSTNAME=%s\n % (hostname))
+statestore.backup_state('network', 'hostname', m.group(1))
+else:
+new_config.write(line)
+new_config.flush()
+# At this point new_config is closed but not removed due to 'delete=False' 
above
+# Now, install the temporary file as configuration and ensure old version 
is available as .orig
+# While .orig file 

Re: [Freeipa-devel] [PATCH] 0008 Modify /etc/sysconfig/network on a client when IPA manages hostname

2011-07-29 Thread Alexander Bokovoy
On 29.07.2011 14:53, Alexander Bokovoy wrote:
 On 29.07.2011 13:52, Martin Kosek wrote:
 Oh, this is not informative at all. I'll get this updated.
 Updated patch attached.
 Ok, hostname is properly changed now. I still have some issues:
Updated again to use more reliable regexp for parsing. Thanks to
ConfParse project for inspiration
(http://code.google.com/p/confparse/source/browse/trunk/confparse.py)

-- 
/ Alexander Bokovoy
From 457b165f50949e4d7d8266de69c4f983424b020d Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy aboko...@redhat.com
Date: Tue, 19 Jul 2011 15:33:53 +0300
Subject: [PATCH] Modify /etc/sysconfig/network on a client when IPA manages
 hostname

https://fedorahosted.org/freeipa/ticket/1368
---
 ipa-client/ipa-install/ipa-client-install |   62 +++--
 ipa-client/man/ipa-client-install.1   |2 +-
 2 files changed, 59 insertions(+), 5 deletions(-)

diff --git a/ipa-client/ipa-install/ipa-client-install 
b/ipa-client/ipa-install/ipa-client-install
index 
2e1a28ca087dee9eea04ccc7a9e6e4f8ce89..afe302f25ed64a8b2792fc76817b178ba1cc855c
 100755
--- a/ipa-client/ipa-install/ipa-client-install
+++ b/ipa-client/ipa-install/ipa-client-install
@@ -28,10 +28,11 @@ try:
 import logging
 import tempfile
 import getpass
+import re
 from ipaclient import ipadiscovery
 import ipaclient.ipachangeconf
 import ipaclient.ntpconf
-from ipapython.ipautil import run, user_input, CalledProcessError, 
file_exists
+from ipapython.ipautil import run, user_input, CalledProcessError, 
file_exists, install_file
 from ipapython import ipautil
 from ipapython import dnsclient
 from ipapython import sysrestore
@@ -87,7 +88,9 @@ def parse_options():
 parser.add_option(, --uninstall, dest=uninstall, action=store_true,
   default=False, help=uninstall an existing installation)
 parser.add_option(, --hostname, dest=hostname,
-  help=The hostname of this server (FQDN). By default of 
nodename from uname(2) is used.)
+  help=The hostname of this server (FQDN). If specified, 
the hostname will be set and 
+   the system configuration will be updated to 
persist over reboot. 
+   By default a nodename result from uname(2) is 
used.)
 parser.add_option(, --enable-dns-updates, dest=dns_updates, 
action=store_true, default=False,
   help=Configures the machine to attempt dns updates when 
the ip address changes.)
 parser.add_option(--no-krb5-offline-passwords, 
dest=krb5_offline_passwords, action=store_false,
@@ -236,6 +239,12 @@ def uninstall(options, env):
 
 print Restoring client configuration files
 fstore.restore_all_files()
+old_hostname = statestore.restore_state('network','hostname')
+if not hostname is None and old_hostname != hostname:
+try:
+ipautil.run(['/bin/hostname', old_hostname])
+except CalledProcessError, e:
+print sys.stderr, Failed to set this machine hostname to %s 
(%s). % (old_hostname, str(e))
 
 if ipautil.service_is_installed('nscd'):
 try:
@@ -520,6 +529,44 @@ def configure_certmonger(fstore, subject_base, cli_realm, 
hostname, options):
 except:
 print certmonger request for host certificate failed
 
+def backup_and_replace_hostname(fstore, hostname):
+# TODO: this code is for Red Hat-based systems
+#   it need to be rewritten for cross-paltform support
+#   so that different configuration backends would be possible
+#   (GNU/Debian stores this information in a different place)
+network_filename = /etc/sysconfig/network
+# Backup original /etc/sysconfig/network
+fstore.backup_file(network_filename)
+hostname_pattern = re.compile('''
+(^
+\s*
+(?Poption [^\#;]+?)
+(\s*=\s*)
+(?Pvalue  .+?)?
+(\s*((\#|;).*)?)?
+$)''', re.VERBOSE)
+temp_filename = None
+with tempfile.NamedTemporaryFile(delete=False) as new_config:
+temp_filename = new_config.name
+with open(network_filename, 'r') as f:
+for line in f:
+m = hostname_pattern.match(line)
+option, value = m.group('option', 'value')
+if option is 'HOSTNAME':
+new_config.write(HOSTNAME=%s\n % (hostname))
+statestore.backup_state('network', 'hostname', value)
+else:
+new_config.write(line)
+new_config.flush()
+# At this point new_config is closed but not removed due to 'delete=False' 
above
+# Now, install the temporary file as configuration and ensure old version 
is available as .orig
+# While .orig file is not used during uninstall, it is left there for 
administrator.
+install_file(temp_filename, network_filename)
+try:
+

Re: [Freeipa-devel] [PATCH] 0008 Modify /etc/sysconfig/network on a client when IPA manages hostname

2011-07-29 Thread Martin Kosek
On Fri, 2011-07-29 at 16:05 +0300, Alexander Bokovoy wrote:
 On 29.07.2011 14:53, Alexander Bokovoy wrote:
  On 29.07.2011 13:52, Martin Kosek wrote:
  Oh, this is not informative at all. I'll get this updated.
  Updated patch attached.
  Ok, hostname is properly changed now. I still have some issues:
 Updated again to use more reliable regexp for parsing. Thanks to
 ConfParse project for inspiration
 (http://code.google.com/p/confparse/source/browse/trunk/confparse.py)
 

Hm, the new regex looks robust.

1) But it didn't find hostname in my case:

# ipa-client-install --server=vm-059.idm.lab.bos.redhat.com
--domain=idm.lab.bos.redhat.com --hostname=foo.idm.lab.bos.redhat.com
...
Client configuration complete.

No sysrestore.state was created, i.e. no hostname was backup-ed.

# ls /var/lib/ipa-client/sysrestore/
688988a01b73872d-networkd35eec0a8128e435-krb5.conf
eada0d7ba116bfd7-sssd.conf
9cf989cb60307725-ntp.conf   e4d8b217dfce5043-ntpd   sysrestore.index

IPA client then failed with an exception because we didn't have the
hostname:

# ipa-client-install --uninstall --unattended
Unenrolling client from IPA server
Removing Kerberos service principals from /etc/krb5.keytab
Disabling client Kerberos and LDAP configurations
Restoring client configuration files
Traceback (most recent call last):
  File /usr/sbin/ipa-client-install, line 1071, in module
sys.exit(main())
  File /usr/sbin/ipa-client-install, line 737, in main
return uninstall(options, env)
  File /usr/sbin/ipa-client-install, line 245, in uninstall
ipautil.run(['/bin/hostname', old_hostname])
  File /usr/lib/python2.7/site-packages/ipapython/ipautil.py, line
218, in run
close_fds=True, env=env)
  File /usr/lib64/python2.7/subprocess.py, line 672, in __init__
errread, errwrite)
  File /usr/lib64/python2.7/subprocess.py, line 1202, in
_execute_child
raise child_exception
TypeError: coercing to Unicode: need string or buffer, NoneType found

There should be a check that if we don't have the hostname, we don't
restore it.

My network configration was:
# cat /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=vm-131.idm.lab.bos.redhat.com

2) Why do we call for backup_and_replace_hostname() only in
configure_sssd_conf()? If we run client installation with --no-sssd,
hostname wouldn't get backup-ed.

Martin

___
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel


Re: [Freeipa-devel] [PATCH] 0008 Modify /etc/sysconfig/network on a client when IPA manages hostname

2011-07-29 Thread Alexander Bokovoy
On 29.07.2011 16:25, Martin Kosek wrote:
 On Fri, 2011-07-29 at 16:05 +0300, Alexander Bokovoy wrote:
 On 29.07.2011 14:53, Alexander Bokovoy wrote:
 On 29.07.2011 13:52, Martin Kosek wrote:
 Oh, this is not informative at all. I'll get this updated.
 Updated patch attached.
 Ok, hostname is properly changed now. I still have some issues:
 Updated again to use more reliable regexp for parsing. Thanks to
 ConfParse project for inspiration
 (http://code.google.com/p/confparse/source/browse/trunk/confparse.py)

 
 Hm, the new regex looks robust.
 
 1) But it didn't find hostname in my case:
There was wrong comparison (I wanted to check if option is not None and
then compare it to 'HOSTNAME' but brain short-circuited. My bad.

 # ipa-client-install --server=vm-059.idm.lab.bos.redhat.com
 --domain=idm.lab.bos.redhat.com --hostname=foo.idm.lab.bos.redhat.com
 ...
 Client configuration complete.
 
 No sysrestore.state was created, i.e. no hostname was backup-ed.
 
 # ls /var/lib/ipa-client/sysrestore/
 688988a01b73872d-networkd35eec0a8128e435-krb5.conf
 eada0d7ba116bfd7-sssd.conf
 9cf989cb60307725-ntp.conf   e4d8b217dfce5043-ntpd   sysrestore.index
 
 IPA client then failed with an exception because we didn't have the
 hostname:
 
 # ipa-client-install --uninstall --unattended
 Unenrolling client from IPA server
 Removing Kerberos service principals from /etc/krb5.keytab
 Disabling client Kerberos and LDAP configurations
 Restoring client configuration files
 Traceback (most recent call last):
   File /usr/sbin/ipa-client-install, line 1071, in module
 sys.exit(main())
   File /usr/sbin/ipa-client-install, line 737, in main
 return uninstall(options, env)
   File /usr/sbin/ipa-client-install, line 245, in uninstall
 ipautil.run(['/bin/hostname', old_hostname])
   File /usr/lib/python2.7/site-packages/ipapython/ipautil.py, line
 218, in run
 close_fds=True, env=env)
   File /usr/lib64/python2.7/subprocess.py, line 672, in __init__
 errread, errwrite)
   File /usr/lib64/python2.7/subprocess.py, line 1202, in
 _execute_child
 raise child_exception
 TypeError: coercing to Unicode: need string or buffer, NoneType found
 
 There should be a check that if we don't have the hostname, we don't
 restore it.
Added both checks (it was there in uninstall but checking hostname
instead of old_hostname).

-- 
/ Alexander Bokovoy
From fede69202b873d11400e59a4de615594981aecba Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy aboko...@redhat.com
Date: Tue, 19 Jul 2011 15:33:53 +0300
Subject: [PATCH] Modify /etc/sysconfig/network on a client when IPA manages
 hostname

https://fedorahosted.org/freeipa/ticket/1368
---
 ipa-client/ipa-install/ipa-client-install |   65 +++--
 ipa-client/man/ipa-client-install.1   |2 +-
 2 files changed, 62 insertions(+), 5 deletions(-)

diff --git a/ipa-client/ipa-install/ipa-client-install 
b/ipa-client/ipa-install/ipa-client-install
index 
2e1a28ca087dee9eea04ccc7a9e6e4f8ce89..8a49ce316db3e1765168af41ee2b4a9c9bf6db2c
 100755
--- a/ipa-client/ipa-install/ipa-client-install
+++ b/ipa-client/ipa-install/ipa-client-install
@@ -28,10 +28,11 @@ try:
 import logging
 import tempfile
 import getpass
+import re
 from ipaclient import ipadiscovery
 import ipaclient.ipachangeconf
 import ipaclient.ntpconf
-from ipapython.ipautil import run, user_input, CalledProcessError, 
file_exists
+from ipapython.ipautil import run, user_input, CalledProcessError, 
file_exists, install_file
 from ipapython import ipautil
 from ipapython import dnsclient
 from ipapython import sysrestore
@@ -87,7 +88,9 @@ def parse_options():
 parser.add_option(, --uninstall, dest=uninstall, action=store_true,
   default=False, help=uninstall an existing installation)
 parser.add_option(, --hostname, dest=hostname,
-  help=The hostname of this server (FQDN). By default of 
nodename from uname(2) is used.)
+  help=The hostname of this server (FQDN). If specified, 
the hostname will be set and 
+   the system configuration will be updated to 
persist over reboot. 
+   By default a nodename result from uname(2) is 
used.)
 parser.add_option(, --enable-dns-updates, dest=dns_updates, 
action=store_true, default=False,
   help=Configures the machine to attempt dns updates when 
the ip address changes.)
 parser.add_option(--no-krb5-offline-passwords, 
dest=krb5_offline_passwords, action=store_false,
@@ -236,6 +239,12 @@ def uninstall(options, env):
 
 print Restoring client configuration files
 fstore.restore_all_files()
+old_hostname = statestore.restore_state('network','hostname')
+if not old_hostname is None and old_hostname != hostname:
+try:
+ipautil.run(['/bin/hostname', old_hostname])
+except CalledProcessError, e:
+print 

Re: [Freeipa-devel] [PATCH] 0008 Modify /etc/sysconfig/network on a client when IPA manages hostname

2011-07-29 Thread Alexander Bokovoy
On 29.07.2011 17:06, Alexander Bokovoy wrote:
 There was wrong comparison (I wanted to check if option is not None and
 then compare it to 'HOSTNAME' but brain short-circuited. My bad.
... and one more update, to get common style for comparisons.

-- 
/ Alexander Bokovoy
From debdf588069ec1c06c29854b80358302470616e0 Mon Sep 17 00:00:00 2001
From: Alexander Bokovoy aboko...@redhat.com
Date: Tue, 19 Jul 2011 15:33:53 +0300
Subject: [PATCH] Modify /etc/sysconfig/network on a client when IPA manages
 hostname

https://fedorahosted.org/freeipa/ticket/1368
---
 ipa-client/ipa-install/ipa-client-install |   65 +++--
 ipa-client/man/ipa-client-install.1   |2 +-
 2 files changed, 62 insertions(+), 5 deletions(-)

diff --git a/ipa-client/ipa-install/ipa-client-install 
b/ipa-client/ipa-install/ipa-client-install
index 
2e1a28ca087dee9eea04ccc7a9e6e4f8ce89..c5f66be85361ecb3ab8b0c41908d378702df068d
 100755
--- a/ipa-client/ipa-install/ipa-client-install
+++ b/ipa-client/ipa-install/ipa-client-install
@@ -28,10 +28,11 @@ try:
 import logging
 import tempfile
 import getpass
+import re
 from ipaclient import ipadiscovery
 import ipaclient.ipachangeconf
 import ipaclient.ntpconf
-from ipapython.ipautil import run, user_input, CalledProcessError, 
file_exists
+from ipapython.ipautil import run, user_input, CalledProcessError, 
file_exists, install_file
 from ipapython import ipautil
 from ipapython import dnsclient
 from ipapython import sysrestore
@@ -87,7 +88,9 @@ def parse_options():
 parser.add_option(, --uninstall, dest=uninstall, action=store_true,
   default=False, help=uninstall an existing installation)
 parser.add_option(, --hostname, dest=hostname,
-  help=The hostname of this server (FQDN). By default of 
nodename from uname(2) is used.)
+  help=The hostname of this server (FQDN). If specified, 
the hostname will be set and 
+   the system configuration will be updated to 
persist over reboot. 
+   By default a nodename result from uname(2) is 
used.)
 parser.add_option(, --enable-dns-updates, dest=dns_updates, 
action=store_true, default=False,
   help=Configures the machine to attempt dns updates when 
the ip address changes.)
 parser.add_option(--no-krb5-offline-passwords, 
dest=krb5_offline_passwords, action=store_false,
@@ -236,6 +239,12 @@ def uninstall(options, env):
 
 print Restoring client configuration files
 fstore.restore_all_files()
+old_hostname = statestore.restore_state('network','hostname')
+if old_hostname is not None and old_hostname != hostname:
+try:
+ipautil.run(['/bin/hostname', old_hostname])
+except CalledProcessError, e:
+print sys.stderr, Failed to set this machine hostname to %s 
(%s). % (old_hostname, str(e))
 
 if ipautil.service_is_installed('nscd'):
 try:
@@ -520,6 +529,47 @@ def configure_certmonger(fstore, subject_base, cli_realm, 
hostname, options):
 except:
 print certmonger request for host certificate failed
 
+def backup_and_replace_hostname(fstore, hostname):
+# TODO: this code is for Red Hat-based systems
+#   it need to be rewritten for cross-paltform support
+#   so that different configuration backends would be possible
+#   (GNU/Debian stores this information in a different place)
+network_filename = /etc/sysconfig/network
+# Backup original /etc/sysconfig/network
+fstore.backup_file(network_filename)
+hostname_pattern = re.compile('''
+(^
+\s*
+(?Poption [^\#;]+?)
+(\s*=\s*)
+(?Pvalue  .+?)?
+(\s*((\#|;).*)?)?
+$)''', re.VERBOSE)
+temp_filename = None
+with tempfile.NamedTemporaryFile(delete=False) as new_config:
+temp_filename = new_config.name
+with open(network_filename, 'r') as f:
+for line in f:
+new_line = line
+m = hostname_pattern.match(line)
+if m:
+option, value = m.group('option', 'value')
+if option is not None and option == 'HOSTNAME':
+if value is not None and hostname != value:
+new_line = u'HOSTNAME=%s' % (hostname)
+statestore.backup_state('network', 'hostname', 
value)
+new_config.write(new_line)
+new_config.flush()
+
+# At this point new_config is closed but not removed due to 'delete=False' 
above
+# Now, install the temporary file as configuration and ensure old version 
is available as .orig
+# While .orig file is not used during uninstall, it is left there for 
administrator.
+install_file(temp_filename, network_filename)
+try:
+

Re: [Freeipa-devel] [PATCH] 0008 Modify /etc/sysconfig/network on a client when IPA manages hostname

2011-07-29 Thread Martin Kosek
On Fri, 2011-07-29 at 17:06 +0300, Alexander Bokovoy wrote:
 On 29.07.2011 16:25, Martin Kosek wrote:
  On Fri, 2011-07-29 at 16:05 +0300, Alexander Bokovoy wrote:
  On 29.07.2011 14:53, Alexander Bokovoy wrote:
  On 29.07.2011 13:52, Martin Kosek wrote:
  Oh, this is not informative at all. I'll get this updated.
  Updated patch attached.
  Ok, hostname is properly changed now. I still have some issues:
  Updated again to use more reliable regexp for parsing. Thanks to
  ConfParse project for inspiration
  (http://code.google.com/p/confparse/source/browse/trunk/confparse.py)
 
  
  Hm, the new regex looks robust.
  
  1) But it didn't find hostname in my case:
 There was wrong comparison (I wanted to check if option is not None and
 then compare it to 'HOSTNAME' but brain short-circuited. My bad.
 
  # ipa-client-install --server=vm-059.idm.lab.bos.redhat.com
  --domain=idm.lab.bos.redhat.com --hostname=foo.idm.lab.bos.redhat.com
  ...
  Client configuration complete.
  
  No sysrestore.state was created, i.e. no hostname was backup-ed.
  
  # ls /var/lib/ipa-client/sysrestore/
  688988a01b73872d-networkd35eec0a8128e435-krb5.conf
  eada0d7ba116bfd7-sssd.conf
  9cf989cb60307725-ntp.conf   e4d8b217dfce5043-ntpd   sysrestore.index
  
  IPA client then failed with an exception because we didn't have the
  hostname:
  
  # ipa-client-install --uninstall --unattended
  Unenrolling client from IPA server
  Removing Kerberos service principals from /etc/krb5.keytab
  Disabling client Kerberos and LDAP configurations
  Restoring client configuration files
  Traceback (most recent call last):
File /usr/sbin/ipa-client-install, line 1071, in module
  sys.exit(main())
File /usr/sbin/ipa-client-install, line 737, in main
  return uninstall(options, env)
File /usr/sbin/ipa-client-install, line 245, in uninstall
  ipautil.run(['/bin/hostname', old_hostname])
File /usr/lib/python2.7/site-packages/ipapython/ipautil.py, line
  218, in run
  close_fds=True, env=env)
File /usr/lib64/python2.7/subprocess.py, line 672, in __init__
  errread, errwrite)
File /usr/lib64/python2.7/subprocess.py, line 1202, in
  _execute_child
  raise child_exception
  TypeError: coercing to Unicode: need string or buffer, NoneType found
  
  There should be a check that if we don't have the hostname, we don't
  restore it.
 Added both checks (it was there in uninstall but checking hostname
 instead of old_hostname).
 

ACK. Before pushing, I just replaced constructs like not var is None
with var is not None - its more pythonic.

Pushed to master.

Martin

___
Freeipa-devel mailing list
Freeipa-devel@redhat.com
https://www.redhat.com/mailman/listinfo/freeipa-devel