<s> '''<STR_LIT>''' <EOL> import StringIO <EOL> from provy . core import Role <EOL> class UserRole ( Role ) : <EOL> '''<STR_LIT>''' <EOL> def group_exists ( self , group_name ) : <EOL> '''<STR_LIT>''' <EOL> values = self . __first_values_from ( '<STR_LIT>' ) <EOL> return group_name in values <EOL> def __first_values_from ( self , basename ) : <EOL> values = self . execute ( "<STR_LIT>" % basename , stdout = False , sudo = True ) <EOL> values = values . strip ( ) . split ( ) <EOL> return values <EOL> def user_exists ( self , username ) : <EOL> '''<STR_LIT>''' <EOL> values = self . __first_values_from ( '<STR_LIT>' ) <EOL> return username in values <EOL> def user_in_group ( self , username , group_name ) : <EOL> '''<STR_LIT>''' <EOL> raw_groups = self . execute ( '<STR_LIT>' % username , sudo = True , stdout = False ) . strip ( ) <EOL> if not raw_groups . startswith ( username ) : <EOL> raise ValueError ( "<STR_LIT>" % username ) <EOL> groups_string = raw_groups . replace ( '<STR_LIT>' % username , '<STR_LIT>' ) <EOL> groups = groups_string . split ( ) <EOL> return group_name in groups <EOL> def ensure_group ( self , group_name , group_id = None ) : <EOL> '''<STR_LIT>''' <EOL> if not self . group_exists ( group_name ) : <EOL> self . log ( "<STR_LIT>" % group_name ) <EOL> if not group_id : <EOL> self . execute ( '<STR_LIT>' % group_name , stdout = False , sudo = True ) <EOL> else : <EOL> self . execute ( '<STR_LIT>' % ( group_id , group_name ) , stdout = False , sudo = True ) <EOL> self . log ( "<STR_LIT>" % group_name ) <EOL> def ensure_user_groups ( self , username , groups = [ ] ) : <EOL> for user_group in groups : <EOL> if not self . user_in_group ( username , user_group ) : <EOL> self . log ( "<STR_LIT>" % ( username , user_group ) ) <EOL> self . execute ( '<STR_LIT>' % ( user_group , username ) , stdout = False , sudo = True ) <EOL> self . log ( "<STR_LIT>" % ( username , user_group ) ) <EOL> def ensure_user ( self , username , identified_by = None , home_folder = None , <EOL> default_script = "<STR_LIT>" , groups = [ ] , is_admin = False , password_encrypted = False ) : <EOL> '''<STR_LIT>''' <EOL> admin_group = self . __get_admin_group ( ) <EOL> self . __ensure_initial_groups ( groups , username ) <EOL> if not self . user_exists ( username ) : <EOL> self . __create_new_user ( username , home_folder , groups , default_script , is_admin ) <EOL> elif is_admin and not self . user_in_group ( username , admin_group ) : <EOL> self . __set_user_as_admin ( username , admin_group ) <EOL> self . ensure_user_groups ( username , groups ) <EOL> self . set_user_password ( username , identified_by , password_encrypted ) <EOL> self . context [ '<STR_LIT>' ] = username <EOL> def __set_user_as_admin ( self , username , admin_group ) : <EOL> self . log ( "<STR_LIT>" % username ) <EOL> self . execute ( '<STR_LIT>' % ( admin_group , username ) , stdout = False , sudo = True ) <EOL> self . log ( "<STR_LIT>" % username ) <EOL> def __create_new_user ( self , username , home_folder , groups , default_script , is_admin ) : <EOL> is_admin_command = "<STR_LIT>" . format ( self . __get_admin_group ( ) ) <EOL> command = "<STR_LIT>" <EOL> home_folder = home_folder or '<STR_LIT>' % username <EOL> group = groups and groups [ <NUM_LIT:0> ] or username <EOL> self . log ( "<STR_LIT>" % username ) <EOL> self . execute ( command % { <EOL> '<STR_LIT>' : group or username , <EOL> '<STR_LIT>' : is_admin and is_admin_command or '<STR_LIT>' , <EOL> '<STR_LIT>' : home_folder , <EOL> '<STR_LIT>' : default_script , <EOL> '<STR_LIT:username>' : username <EOL> } , stdout = False , sudo = True ) <EOL> self . log ( "<STR_LIT>" % username ) <EOL> def __ensure_initial_groups ( self , groups , username ) : <EOL> for user_group in groups : <EOL> self . ensure_group ( user_group ) <EOL> if not groups : <EOL> self . ensure_group ( username ) <EOL> def __get_admin_group ( self ) : <EOL> distro_info = self . get_distro_info ( ) <EOL> if distro_info . distributor_id == '<STR_LIT>' : <EOL> admin_group = '<STR_LIT>' <EOL> else : <EOL> admin_group = '<STR_LIT>' <EOL> return admin_group <EOL> def set_user_password ( self , username , password , encrypted = False ) : <EOL> """<STR_LIT>""" <EOL> tmp_file = self . create_remote_temp_file ( ) <EOL> password_line = "<STR_LIT>" . format ( <EOL> username = username , <EOL> password = password <EOL> ) <EOL> self . put_file ( StringIO . StringIO ( password_line ) , tmp_file , sudo = True , stdout = False ) <EOL> command = '<STR_LIT>' . format ( <EOL> encrypted = "<STR_LIT>" if encrypted else "<STR_LIT>" , <EOL> tmp_file = tmp_file <EOL> ) <EOL> self . execute ( command , stdout = False , sudo = True ) <EOL> self . remove_file ( tmp_file , sudo = True ) </s>
