tirer son épingle du jeu

Etymology
lit. 'to pull one's pin out of the game', a reference to a 15th century children's game with pins and a ball, similar to mikado.

Verb

 * 1)  to play one's cards right; to make it out of a potentially bad situation relatively unscathed; to pull out skilfully while the going is still good