3 \�me� � @ s� U d Z ddlZddlZddlZddlmZmZ ddlmZ ddl m Z ddlmZm Z ddlmZ dZd gZeZd ddeed geg d�Zee e�Z eje�Zdd� Zdd� Zee eedd�dd�ZdS )z8Migrator: Migrate old versions of cloud-init data to new� N)�helpers�util)�Cloud)�Config)� MetaSchema�get_meta_doc)� PER_ALWAYSaE This module handles moving old versions of cloud-init data to newer ones. Currently, it only handles renaming cloud-init's per-frequency semaphore files to canonicalized name and renaming legacy semaphore names to newer ones. This module is enabled by default, but can be disabled by specifying ``migrate: false`` in config. �allZcc_migratorZMigratorz.Migrate old versions of cloud-init data to newz6# Do not migrate cloud-init semaphores migrate: false )�id�name�title�description�distrosZexamples� frequencyZactivate_by_schema_keysc C s� | j jd�| j jd�f}d}x�|D ]�}| s"tjj|� r<q"xttj|�D ]f}tjj||�}tjj|�rHtjj |�\}}t j|�}||krHtjj||| �} tj || � |d7 }qHW q"W |S )N�semr � )�paths� get_ipath� get_cpath�os�path�exists�listdir�join�isfile�splitextr �canon_sem_name�shutilZmove) �cloudr Zam_adjusted�sem_path�p� full_pathr ZextZ canon_name�new_path� r# �!/usr/lib/python3.6/cc_migrator.py�_migrate_canon_sems- s r% c C s& dddgi}| j jd�| j jd�f}x�|D ]�}| s*tjj|� rDq*tj|�}x�|j� D ]�\}}|tj |�g}g }x@tj |�D ]2} tjj| �\} }| |kr~tjj| �r~|j | � q~W xd|D ]\} tjtjj|| �� tjj| �\}} x2|D ]*}tjd| |� |j|| �� W d Q R X q�W q�W qXW q*W d S )Nzapt-update-upgradeZ apt_configureZpackage_update_upgrade_installr z*Migrating %s => %s with the same frequency)r r r r r r r ZFileSemaphores�itemsr r r r �appendr Zdel_filer �LOG�debug�lock)r Z legacy_adjustr r Z sem_helperZmod_nameZ migrate_toZ possiblesZ old_existsr r Z_ext�_nameZfreq�mr# r# r$ �_migrate_legacy_sems? s. r- )r �cfgr �args�returnc C sH t j|dd�}t j|�s(tjd| � d S t|�}tjd|� t|� d S )NZmigrateTz,Skipping module named %s, migration disabledz8Migrated %s semaphore files to there canonicalized names)r Zget_cfg_option_strZtranslate_boolr( r) r% r- )r r. r r/ Z do_migrateZ sems_movedr# r# r$ �handle] s r1 )�__doc__Zloggingr r Z cloudinitr r Zcloudinit.cloudr Zcloudinit.configr Zcloudinit.config.schemar r Zcloudinit.settingsr ZMODULE_DESCRIPTIONr r �metaZ getLogger�__name__r( r% r- �str�listr1 r# r# r# r$ �<module> s0